maynull
maynull

Reputation: 2046

How to select certain rows of a dataframe in Python?

I have a pandas dataframe that looks like this:

              x_cor
y_cor             
893.200012       1
893.299988      17
893.400024      41
893.500000      39
893.599976      40
893.700012      36
893.799988       2
893.900024      13
894.000000      44
894.099976      43
894.200012      74
894.299988      88
894.400024      78
894.500000     132
894.599976     180
894.700012     178

What I want to do is to select certain rows, according to a condition, and to create 2 different dataframes out of it(one of which is comprised of the rows that meet the condition and the other of which consists of the rows that don't meet the condition). The condition is whether or not the x_cor value of each row is bigger than the preceding and subseqent x_cor values.

For example, the 3rd row 893.400024 41 meets the condition because the previous row's x_cor is 17 and the next row's x_cor is 39, which are smaller than 41.

I think it would be inefficient if I used loops with iloc or ix. What would be a better way to do this?

Upvotes: 0

Views: 234

Answers (2)

BENY
BENY

Reputation: 323366

From scipy argrelextrema

from scipy.signal import argrelextrema
df.iloc[argrelextrema(df.x_cor.values, np.greater)]
Out[981]: 
            x_cor
y_cor            
893.400024     41
893.599976     40
894.000000     44
894.299988     88
894.599976    180

Upvotes: 3

user3483203
user3483203

Reputation: 51175

Use shift

df.loc[(df.x_cor > df.x_cor.shift(1)) & (df.x_cor > df.x_cor.shift(-1))]

         y_cor  x_cor
2   893.400024     41
4   893.599976     40
8   894.000000     44
11  894.299988     88
14  894.599976    180

Upvotes: 3

Related Questions