简体   繁体   English

将类型为object的pandas dataframe列转换为numpy数组

[英]Convert a pandas dataframe column of type object to a numpy array

I have a pandas dataframe that holds the image id, image class and image data: 我有一个熊猫数据框,其中包含图像ID,图像类和图像数据:

img_train.head(5)

   ID  index  class                                               data
0  10472  10472      0  [[[255, 255, 255, 0], [255, 255, 255, 0], [255...
1   7655   7655      0  [[[255, 255, 255, 0], [255, 255, 255, 0], [255...
2   6197   6197      0  [[[255, 255, 255, 0], [255, 255, 255, 0], [255...
3   9741   9741      0  [[[255, 255, 255, 0], [255, 255, 255, 0], [255...
4   9169   9169      0  [[[255, 255, 255, 0], [255, 255, 255, 0], [255...

I am trying to convert each of these columns to a numpy array: 我试图将这些列中的每一个转换为一个numpy数组:

train_img_array = np.array([])
train_id_array = np.array([])
train_lab_array = np.array([])
count = 0
for index, row in img_train.iterrows():
    imgid = row['ID']
    imgclass = row['class']
    imgdata = row['data']
    #print(imgdata)
    train_img_array = np.append(train_img_array, imgdata )
    train_lab_array = np.append(train_lab_array, imgclass )
    train_id_array = np.append(train_id_array, imgid )

However, the the column that holds the image data and is of the type 'object' is not getting translated into corresponding row in the numpy array. 但是,保存图像数据且类型为“对象”的列不会转换为numpy数组中的相应行。 For instance, this is the shape of each numpy array after processing 58 rows from the original dataframe: 例如,这是处理原始数据帧中的58行后每个numpy数组的形状:

train_img_array.shape
train_lab_array.shape
train_id_array.shape
(93615200,)
(58,)
(58,)

How do i fix this? 我该如何解决?

I have found the answer to this question. 我已经找到了这个问题的答案。 It's rather very straight forward and i just did not see it to begin with. 这非常简单,我只是没有看到它的开始。 This is how i get the object data as well in to numpy array (.values :) ) 这就是我如何将对象数据也放入numpy数组(.values :))

train_img_array = np.array([])
train_id_array = np.array([])
train_lab_array = np.array([])
train_id_array = img_train['ID'].values
train_lab_array = img_train['class'].values
train_img_array =img_train['data'].values
#train_img_array = np.row_stack(img_train['data'])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM