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

Python Pandas: Select inverse of dataframe grouby

Say I have a pandas data frame called df_all.
I then wish to group by columns Foo, and select only the last row for each grouped by set:

# E.g. 
# Foo Bar Baz
#  1   1   1
#  1   2   2
#  2   1   2
#  2   3   4
#  2   5   6
# Wish to select rows '1 2 3' and '2 5 6' since if we group by Foo,
# they are the last fow for each distinct Foo value
df_slice = df_all.groupby('Foo').last()

The above works, now I wish to have the set of rows that are in df_all, and not in df_slice, this is what I tried:

dv_inverse = df[~df_slice.isin(df_all)].dropna(how = 'all')

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 :

What about:

df_inverse = df_all[df_all.duplicated(subset='Foo', keep='last')] 
print(df_inverse)
   Foo  Bar  Baz
0    1    1    1
2    2    1    2
3    2    3    4
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