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

How to append dictionary rows into empty pandas dataframe?

I have a dataframe df and and empty dataframes df1 and df2.
I am trying to append rows if conditions are true in df1 or df2


##df is my input dataframe

#df1 and df2 are empty dataframe
df1 = pd.DataFrame(columns = df.columns)
df2 = pd.DataFrame(columns = df.columns)


df_dict = df.to_dict('records')
for rows in df_dict:
     if condition1 == true:
       print(rows)
       df1.append(rows, ignore_index = True)

     else:
       print(rows)
       df2.append(rows, ignore_index = True)

print is returning rows but append is empty

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 need to assign df1 and df2 again for it works, else the value of df1 and df2 will not be updated. Beside, you it must be df1.append, not df1.appned

##df is my input dataframe

#df1 and df2 are empty dataframe
df1 = pd.DataFrame(columns = df.columns)
df2 = pd.DataFrame(columns = df.columns)


df_dict = df.to_dict('records')
for row in df_dict:
    if conditions1 == true:
        print(row)
        df1 = df1.append(row, ignore_index = True)
    else:
        print(row)
        df2 = df2.append(row, ignore_index = True)

print(df1)
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