Reputation: 69
I have a MultiIndex dataframe for which I would like to create a conditional statement that would say in each specific group of the columns "A" if the value in column "F" is > 0, then, that entire group (of column A) should be copied to a new dataframe (newdf) for further processing.
Here is how two rows of df kind of looks like:
A B C D E F G H
A8374D []KLJEFSIJSE some;text more,text 12.4 4.6 13 X5
[]BKFLKJLJLK text;some other,stuff 65.0 6.5 11 Y0
...
Here is what I have so far (which doesn't work)
for row in df['A']:
newdf = df['F'].where(df['F'] > 0)
print(newdf)
Error message:
KeyError: 'A'
Upvotes: 2
Views: 206
Reputation: 481
I guess A is one of your multiindex. I would like to make A not as index
df = df.reset_index()
and then I guess you want to keep A if ANY of F within group A is > 0
df_new = df.groupby('A').apply(lambda x: (x.F > 0).any()).to_frame('WANT').reset_index()
df_final = df[df.A.isin(df_new.loc[df_new.WANT == 1,'A'])]
If you want to make sure ALL F values within group A is >0, then change to
(x.F > 0).all()
Please let me know if you have any other questions. If you like it, please vote me up
Upvotes: 1