tubadc
tubadc

Reputation: 772

Add numpy arrays as rows into pandas with string as index

I've a list of strings of images names and a numpy array with shape (4096,) features for each image.

for example the list of names:

img_list = ['img1', 'img2', 'img3']

features np.array

[array([0.        , 0.04688909, 0.05700445, ..., 0.        , 0.        ,
        0.        ], dtype=float32),
 array([0.        , 0.04688909, 0.05700445, ..., 0.        , 0.        ,
        0.        ], dtype=float32),
 array([0.03546982, 0.00310931, 0.        , ..., 0.        , 0.01513313,
        0.        ], dtype=float32)]

i want to add them to a pandas dataframe where the index would be the img name and features as 1 by column, something like this:

    f_1 f_2 f_3 ... f_4094  f_4095  f_4096
img 

The index from list of imgs is the same as the features array, so the img[0] has the features[0] array and so on.

How can I manage to merge them into a single dataframe shape N x 4096?

Thanks!

Upvotes: 2

Views: 2248

Answers (1)

piRSquared
piRSquared

Reputation: 294338

IIUC:

pd.DataFrame(features, img_list).add_prefix('f_')

MCVE

img_list = ['img1', 'img2', 'img3']
features = [np.array([1, 2, 3]), np.array([4, 5, 6]), np.array([7, 8, 9])]

df = pd.DataFrame(features, img_list).add_prefix('f_')
df

      f_0  f_1  f_2
img1    1    2    3
img2    4    5    6
img3    7    8    9

Upvotes: 4

Related Questions