1

I want to create a new dataframe having only those rows which has name in the Image_list. Original df has records more than 200,000 whereas Image_list has only 300 list of image names.

for name in Image_list:
    df1= df.loc[df['ID']== name]
    print(df1)
    Final_data.append(df1)

Final_data.shape

'ID' column contains much more than Image_list, for example

['Black_Hair',
 'Eyeglasses',
 'Male',
 'Smiling',
 'Straight_Hair',
 'Wearing_Earrings',
 'Wearing_Necktie']
4
  • should use df1= df[df['ID']==name] Commented May 29, 2022 at 13:02
  • changes but data is not being copied in new dataframe Commented May 29, 2022 at 13:07
  • and need to assign the new data: Final_data = Final_data.append(df1) Commented May 29, 2022 at 13:09
  • Also, should use pd.concat() instead of df.append(), see my answer Commented May 29, 2022 at 13:17

1 Answer 1

1

You would need to assign the new data to the existing df Final_data. Also, the method df.append() is deprecated. Should use pd.concat() instead

import pandas as pd

Final_data = pd.DataFrame()

for name in Image_list:
    df1 = df[df['ID']==name]
    print(df1.shape)
    Final_data = pd.concat([Final_data, df1])

print(Final_data.shape)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.