6
votes

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?

1
Are you after last_valid_index ? - Jon Clements♦
no not the last index, the last value, in which ever column it is. - Mustard Tiger

1 Answers

11
votes

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

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