Append new row when using pandas iterrows()?

Viewed 14056

I have the following code where I create df['var'2] and alter df['var1']. After performing these changes, I would like to append the newrow (with df['var'2]) to the dataframe while keeping the original (though now altered) row (which has df['var1']).

for i, row in df.iterrows():
    while row['var1'] > 30: 
        newrow = row
        newrow['var2'] = 30
        row['var1'] = row['var1']-30
        df.append(newrow)

I understand that when using iterrows(), row variables are copies instead of views which is why the changes are not being updated in the original dataframe. So, how would I alter this code to actually append newrow to the dataframe?

Thank you!

1 Answers
Related