1

How can I select all rows of a data frame where a condition is met according to a column, which has to do with the relationship between every 2 entries of that column. To give the specific example, lets say I have a DataFrame:

>>>df = pd.DataFrame({'A': [ 1, 2, 3, 4],
                      'B':['spam', 'ham', 'egg', 'foo'],
                      'C':[4, 5, 3, 4]})
>>> df
   A     B  C
0  1  spam  4
1  2   ham  5
2  3   egg  3
3  4   foo  4
>>>df2 = df[ return every row of df where C[i] > C[i-1] ]

>>> df2
   A    B  C
1  2  ham  5
3  4  foo  4

There is plenty of great information about slicing and indexing in the pandas docs and here, but this is a bit more complicated, I think. I could also be going about it wrong. What I'm looking for is the rows of data where the value stored in C is no longer monotonously declining.

Any help is appreciated!

ItsAnApe
  • 412
  • 1
  • 3
  • 12

1 Answers1

2

Use boolean indexing with compare by shifted column values:

print (df[df['C'] > df['C'].shift()])
   A    B  C
1  2  ham  5
3  4  foo  4

Detail:

print (df['C'] > df['C'].shift())
0    False
1     True
2    False
3     True
Name: C, dtype: bool

If want all monotonously declining rows compare diff of column:

print (df[df['C'].diff() > 0])
   A    B  C
1  2  ham  5
3  4  foo  4
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252