Adarsh Ravi
Adarsh Ravi

Reputation: 953

Add a new column to dataframe and add unique values to each row

I have a dataframe with two columns: id_code & diagnosis

I have a folder with images with filename same as id_code. I want to add a new columns to the dataframe: Image, height and width

I have the following code:

for idx, row in df_train.iterrows():
    img = Image.open('train_images/{}.png'.format(row['id_code']))
    height, width = img.size
    row['image'] = img
    row['height'] = height
    row['width'] = width

I tried using df_train.iloc[idx]['image'] = img instead of row['image'] = img and so on for the rest of the lines of code but that would give me an error.

But this code updates the row but doesn't update the dataframe itself.

Upvotes: 1

Views: 1162

Answers (1)

BENY
BENY

Reputation: 323226

We should assign it cell by cell

l=[]
for idx, row in df_train.iterrows():
    img = Image.open('train_images/{}.png'.format(row['id_code']))
    l.append(img)
    height, width = img.size
    df.loc[idx,'height'] = height
    df.loc[idx,'width'] = width

df['img']=l

Upvotes: 2

Related Questions