简体   繁体   中英

First pandas DataFrame column index greater than x

I know to cast pandas DataFrame column to a list (with .tolist() or list() ) and then do what you want, will make it way much slower, so I don't want to use these methods .

I want to find the index of the first element of a pandas DataFrame column which is equal or greater than a value x , in other words >=x . And if there is nothing then return None .

For example, if the column is this and our function is called first_greater() :

    0
0   1
1  -5
2   6
3   4
4  -7
5  12
6  -2
7   0
8  -3

Then we have:

first_greater(-5) = 0
first_greater(7) = 5
first_greater(4) = 2
first_greater(6) = 2
first_greater(22) = None

I'm new to pandas and I don't know how to do this. Any help would be appreciated.

You want to check both if any value in the dataframe is greater than the given value, and also return the first value that satsfies the condition. You have idxmax for that:

def first_greater(df, n, col):
    m = df.col.ge(n)
    return m.any() and m.idxmax() 

Note that in the return statement, the right part of the and is only evaluated if the first condition m.any() is satisfied, otherwise False is returned.


Let's check with the proposed examples:

first_greater(df, 5, 'col1')
# 0

first_greater(df, 7, 'col1')
# 5

first_greater(df, 4, 'col1')
# 2

first_greater(df, 6, 'col1')
# 2

first_greater(df, 22, 'col1')
# False

Input data -

    col1
0     1
1    -5
2     6
3     4
4    -7
5    12
6    -2
7     0
8    -3
s = pd.Series([1, -5, 6, 4, -7, 12, -2, 0, -3])

def first_greater(n):
    condition = (s >= n)
    if condition.any():
        return condition.idxmax()
    else:
        return None

I know you have the answer already. But just another approach to show the possibilities

def fg(n):
try:
    a = df.loc[df.col1.ge(n)].index[0]
    return a
except:
    print('None')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM