redwolf_cr7
redwolf_cr7

Reputation: 2037

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:

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:

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. For instance, this is the shape of each numpy array after processing 58 rows from the original dataframe:

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

How do i fix this?

Upvotes: 0

Views: 7205

Answers (1)

redwolf_cr7
redwolf_cr7

Reputation: 2037

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 :) )

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'])

Upvotes: 1

Related Questions