简体   繁体   中英

Is there a non complicated way to access the first non nan value of a pandas series?

I have a dataframe column with values a mix of nans and integers. My goal is to retrieve the first non-nan value. The column looks like this [nan, nan, 3, nan, 5, ...]. Based on the answers I found on stack overflow I came up with the 2 approaches below. Clearly, example number 1 is way more efficient but it still feels complicated for the goal. Is there a better way to retrieve the first non-nan value of a pandas series than example no1 below?

# the column I want to access is the last in the df
df.iloc[df.col.first_valid_index(),-1] 

59.6 µs ± 2.54 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

df.col.fillna(method='bfill')[0]

208 µs ± 7 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

Welcome, You can try something like this. it's probably easier to understand than the mentioned solutions, The logic is easier to understand, but the main drawback of such method is that it's about 10x slower than your the first solution you mentioned

df[df.col.notna()].iloc[0]

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