Nils
Nils

Reputation: 409

How can i delete rows of a dataframe if the values of a specifing column are not strictly increasing?

Ok, i think this should be pretty simple with an if statement within a list-comprehension. Anyway i don´t know how to handle this.

As you can see there down below, i would like to iterate over the rows and delete these rows where the current value of column "1" is smaller then the value before.

I would like to make a new dataframe just with increasing values. I don´t want to sort my dataframe.

print (df)
                0      1
649  1.244399e-09   9.07
648  1.152221e-09   9.00
647  1.075406e-09   8.96
646  1.013954e-09   8.92
645  9.371397e-10   8.88
644  2.243742e-09   9.57
643  2.113292e-09   9.50
642  1.956752e-09   9.42
641  1.826302e-09   9.37
640  1.721942e-09   9.33
639  1.591492e-09   9.28
638  1.487131e-09   9.23
637  1.408861e-09   9.19
636  1.304501e-09   9.14
635  4.809608e-09  10.32

Upvotes: 0

Views: 379

Answers (2)

jezrael
jezrael

Reputation: 862681

Use Series.diff with filtering by boolean indexing:

#if need first and second value for increasing second value
#df1 = df[df[1].diff().bfill() > 0]
df1 = df[df[1].diff() > 0]
print (df1)
                0      1
644  2.243742e-09   9.57
635  4.809608e-09  10.32

Detail:

print (df[1].diff())
649     NaN
648   -0.07
647   -0.04
646   -0.04
645   -0.04
644    0.69
643   -0.07
642   -0.08
641   -0.05
640   -0.04
639   -0.05
638   -0.05
637   -0.04
636   -0.05
635    1.18
Name: 1, dtype: float64

Upvotes: 2

DiCaprio
DiCaprio

Reputation: 833

l = [x for x in df.index if x > 0 and df[x]['column1'] > df[x - 1]['column1']]

in l you store all the index of rows you want and then proceed with the loc opertor.

df2 = df.loc[l]

Upvotes: 1

Related Questions