0

I have 238 dataframes that look like this:

Index before after
a,1 10 10
b,2 10 100
c,3 100 100
d,4 1000 100

I would like to have a for loop which would drop all of the rows where before and after values are the same (leave only those rows where they are different). Example

Index before after
b,2 10 100
d,4 1000 100

Right now, I just have 238 of these: onlydiffs_dfi = dfi[dfi['before'] != dfi['after']]

Which is obviously not great, and can be accomplished with a for loop but I but figure out how to write it. Please help!

EMC
  • 13
  • 4
  • Do your DFs come from csvs? You can put them in a list and iterate over the list. – 1extralime Jul 20 '22 at 15:26
  • no, I created them using this code (gifted from another stackoverflow genius): https://stackoverflow.com/questions/73041300/use-python-pandas-for-loop-to-create-pivot-tables-for-each-column-in-dataframe?noredirect=1#comment129004997_73041300 – EMC Jul 20 '22 at 15:35

1 Answers1

0

Make a list containing the dataframes and iterate:

df_list =[*list of dfs]

for df in df_list:
    new_df = df[df['before'] != df['after']]

Then you can append it to a new list... or whatever you want to do with it If all your dfs are in a dictionary, you iterate as well just index into it:

df_dict = {key0:df0,key1:df1 ....}
for key,df in df_dict.items():
   new_df = df[df['before'] != df['after']]

or even less pythonic:

for key in df_dict.keys():
    df = df_dict[key]
    new_df = df[df['before'] != df['after']]

You can even convert you dictionary values to a list and use the first method:

df_list = list(df_dict.values())
1extralime
  • 606
  • 3
  • 6