Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 304
Mustard Tiger Avatar asked Feb 06 '18 15:02

Mustard Tiger


People also ask

How do I find the last non null value of a column in Python?

last_valid_index() method. By using this method, we can get the index for the last non-NA/null value. It returns a scalar that is the type of index. It returns None if all elements are non-NA/null and also returns None for empty DataFrame.

Does Pandas mean ignore NaN?

pandas mean() Key PointsBy default ignore NaN values and performs mean on index axis.

How do you find the last index of a data frame?

iloc – Pandas Dataframe. iloc is used to retrieve data by specifying its index. In python negative index starts from the end so we can access the last element of the dataframe by specifying its index to -1.

How do I get rid of NaN in Pandas?

By using dropna() method you can drop rows with NaN (Not a Number) and None values from pandas DataFrame. Note that by default it returns the copy of the DataFrame after removing rows. If you wanted to remove from the existing DataFrame, you should use inplace=True .


1 Answers

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

df['last_value'] = df.ffill(axis=1).iloc[:, -1] 
print (df)
like image 168
Austin Avatar answered Sep 20 '22 22:09

Austin