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?
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.
pandas mean() Key PointsBy default ignore NaN values and performs mean on index axis.
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.
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 .
Get last non NaN
value in each row of a dataframe:
df['last_value'] = df.ffill(axis=1).iloc[:, -1]
print (df)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With