Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Creating new data frame from old dataframe

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading