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

pandas how to filter rows by certain conditions after groupby?

I’d like to filter the rows with a difference less than 20 after groupby.

Here is my original table

A     B     Value
1     1       0
1     2       10
1     2       20
1     2       25
2     1       0
2     1       15
2     1       100

After ‘df.groupby([‘A’, ‘B’])’, it would be

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

A     B     Value
1     1       0           
1     2       10
      2       20
      2       25
2     1       0
      1       15
      1       100

I would like to filter those rows with a difference less than 20.

For example,

Group[A=1][B=1]row1 got no previous row, so filter out.

Group[A=1][B=2]row1 got no previous row, so filter out.

Group[A=1][B=2]row2 got a difference of 10, so keep it.

Group[A=2][B=1]row3 got a difference of 100, so filter out.

The results I expect:

A     B     Value
1     2       20
1     2       25
2     1       15

Here is what I tried:

df.groupby(['A', 'B']).filter(lambda x : (x['Value'] - x['Value'].shift(1) < 20).any())

But it does not work.

Any advice would be greatly appreciated

>Solution :

Doing diff

out = df[df.groupby(['A','B']).Value.diff().lt(20)]
Out[398]: 
   A  B  Value
2  1  2     20
3  1  2     25
5  2  1     15
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