PV8
PV8

Reputation: 6260

Find next higher value in a python dataframe column

we all now how to find the maximum value of a dataframe column.

But how can i find the next higher value in a column? So for example I have the following dataframe:

d = {'col1': [1, 4, 2], 'col2': [3, 4, 3]}
df = pd.DataFrame(data=d)
   col1  col2
0     3     3
1     5     4
2     2     3

Basic-Questions: When I want to find the next higher value in col1 to 0, outcome would be:2. Is there something similar to: df.loc[df['col1'].idxmax()], which would lead to:

col1 col2
5    4

And my outcome should be:

col1 col2
2     3

Background: And I am using a if-condition to filter this dataframe, as I need to prepare it for further filtering, and not all values are exsting which I will put in:

v= 0
if len(df[(df['col1'] == v)]) == 0:
    df2 = df[(df['col1'] == v+1)]
else:
    df2 = df[(df['col1'] == v)]

This would lead to an empty dataframe.

But I would like to go the the next entry not v+1=1 , in this case I want to insert 2because it is the next higher value, which has entry after 0. So the condition would be:

v= 0
if len(df[(df['col1'] == v)]) == 0:
    df2 = df[(df['col1'] == 2)] #the 2 has to be find automatic, as the next value does not have a fixed distance
else:
    df2 = df[(df['col1'] == v)]

How can I achieve that automatically?

So my desired outcome is:

when I put in v=0:

df2
col1 col2
2     3

when I put in v=2, it jumps to v=3:

df2
col1 col2
 3     3

If I put v=3, it stays (else-condition):

df2
col1 col2
 3     3

Upvotes: 1

Views: 1930

Answers (1)

BENY
BENY

Reputation: 323226

Check the searchsorted from numpy

df=df.sort_values('col1')
df.iloc[np.searchsorted(df.col1.values,[0])]
   col1  col2
2     2     3
df.iloc[np.searchsorted(df.col1.values,[3,5])]
   col1  col2
0     3     3
1     5     4

Add-on(from the questioneer): This also skips the if-condition

Upvotes: 4

Related Questions