Home > Blockchain >  python filter groups with the last row in groups are negative
python filter groups with the last row in groups are negative

Time:04-30

I'd like to slice my data frame, based on some group conditions, and get all the groups whose last record has a negative value.

A B C D
1 a a 1
1 a a 2
1 a a 3
2 a a 1
2 a a -1
3 a a -1
3 a a -2
3 a a -3

Suppose this is my data frame, group by column A. I want to get all the groups with a negative last value in column D. output:

A B C D
2 a a 1
2 a a -1
3 a a -1
3 a a -2
3 a a -3

Column B and C are not related to the filter. But I need all the rows in each group, not only the last rows. How to do this?

CodePudding user response:

Use enter image description here

  • Related