Mustard Tiger
Mustard Tiger

Reputation: 3671

Pandas find last non NAN value

I have a pandas data frame where the columns are dates and each row is an independent time series.

I try to get the last value of each row using the following:

df['last'] =  df.iloc[:,-1]

However some rows have NAN values in the last column.

How can I get the last non NAN value in a row?

Upvotes: 8

Views: 7371

Answers (1)

Austin
Austin

Reputation: 26039

Get last non NaN value in each row of a dataframe:

df['last_value'] = df.ffill(axis=1).iloc[:, -1] 
print (df)

Upvotes: 14

Related Questions