0

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

2 Answers 2

1

Don't use append cuz it is depreciated. Instead use concat will solve your problem. Also, you do not need to iterate by row

However, I see your problem is not that complicated. It is simply using .loc:

df1 = df.loc[df['col'] == condition, : ].copy()
df2 = df.loc[~(df['col'] == condition), : ].copy()

If df1 and df2 is not empty dataframe, and you want to add rows meeting condition to them:

tem1 = df.loc[df['col'] == condition, : ].copy()
tem2 = df.loc[~(df['col'] == condition), : ].copy()

# df1:
df1 = pd.concat([df1, tem1])

# df2:
df2 = pd.concat([df2, tem2])
Sign up to request clarification or add additional context in comments.

2 Comments

if condition: append into df1, else append into df2 . This requires looping through ?
@Shubh: from your code, I understand that I can divide df into two groups. Group of rows that meet condition, call gr A, and group of rows not meet condition, call gr B. Instead of iterating each row (which takes a lot of time), you can use .loc to filter those rows that belong to group A (meet condition) as df1 and group B (not meet condition) as `df2'. If I miss understand your question, please elaborate it a bit more.
0

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)

2 Comments

obviously it was Typo
@Shubh it was typo, but you really need to assign df1 again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.