1

我在 numpy 中有一个一维数组,其形状为 (761,),每个条目是一个 10 元组,其元素我无法独立访问。数据类型是

array1.dtype

dtype([('Name', '|S58'), ('Category', '|S32'), ('HQLocation', '|S34'),
       ('YearFounded', '<i8'), ('Latitude', '<f8'), ('Longitude', '<f8'), 
       ('TotalFundingAmount', '<i8'), ('LastFundingAmount', '<i8'), 
       ('Investors', '<i8'), ('NGrams', '|S98')])

示例行是 array1[578]

('"FoxyP2, Inc."', 'Education', '"Cuajimalpa, Mexico"', 2006, 19.3547, 
 -99.3001, 55317213, 42999977, 3, 
 'english;learning;reinvent;experience;english learning')

我试图将其制成一个形状为 (761,10) 的二维数组,同时保留列名和数据类型。

4

1 回答 1

2

It's just impossible with your input, as the different columns don't have the same type: some of them are strings, some are floats, some integers.

NumPy arrays are homogeneous, meaning that all entries must have the same data type. This data type can be simple (int, float, ...) or complicated (like a tuple whose first element is a "|S58", whose fourth is a int, whose fifth is a float...), but in any case, all the entries have the same type. You can get more information in the documentation, here and here.

But why do you need a 2D array? You can access and manipulate each column independently with indexing (eg, your_array[YearFounded] will return your fourth column)...

于 2012-09-05T10:50:49.133 回答