Python - strings and integers in Numpy array

前端 未结 2 512
渐次进展
渐次进展 2021-01-25 17:26

I want to create an array with 3 columns. The first one a string, the other two integers used for calculations. Then more rows will be added through the append function (below).

2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-25 18:20

    To have such a mixed datatype data, we could use object as dtype before appending or stacking -

    a = np.array([["String",1,2]], dtype=object)
    b = [["another string", 3, 4]]
    a = np.vstack((a,np.asarray(b,object)))
    

    Sample run -

    In [40]: a = np.array([["String",1,2]], dtype=object)
    
    In [41]: b = [["another string", 3, 4]]
    
    In [42]: np.vstack((a,np.asarray(b,object)))
    Out[42]: 
    array([['String', 1, 2],
           ['another string', 3, 4]], dtype=object)
    

提交回复
热议问题