timothyylim
timothyylim

Reputation: 1527

Deleting rows that have decreasing values

I am examining what happens to variable X when Y is increasing. I have a dataframe and I want to keep data where Y is increasing for a minimum of 2 months.

Is it possible to loop through a dataframe and drop decreasing rows for Y?

date    | X | Y
---------------
1-2017  | 2 | 1
2-2017  | 2 | 2
3-2017  | 2 | 3
4-2017  | 2 | 4
5-2017  | 2 | 5
6-2017  | 2 | 4
7-2017  | 2 | 3
8-2017  | 2 | 1
9-2017  | 2 | 1
10-2017 | 2 | 4
11-2017 | 2 | 7
12-2017 | 2 | 1
13-2017 | 2 | 1

After stripping decreasing dates:

date    | X | Y
---------------
1-2017  | 2 | 1
2-2017  | 2 | 2
3-2017  | 2 | 3
4-2017  | 2 | 4
5-2017  | 2 | 5
9-2017  | 2 | 1
10-2017 | 2 | 4
11-2017 | 2 | 7

Upvotes: 0

Views: 167

Answers (1)

harpan
harpan

Reputation: 8631

You need:

df.loc[((df['Y']-df['Y'].shift(-1))<0) | ((df['Y']-df['Y'].shift(1))>0)]

Output:

     date   X   Y   
0   1-2017  2   1   
1   2-2017  2   2   
2   3-2017  2   3   
3   4-2017  2   4   
4   5-2017  2   5   
8   9-2017  2   1   
9   10-2017 2   4   
10  11-2017 2   7   

Upvotes: 1

Related Questions