-1

I have a timeseries stored in a dataframe and would like to extract the latest non NAN value per column and store it in a single-row dataframe.

So my datraframe looks as follows:

data={  'col1' : [np.nan,np.nan,3], 'col2' : [1,np.nan,np.nan],'col3' : [np.nan,3,np.nan]}
df = pd.DataFrame(data,index=pd.bdate_range(start='01.01.2020',end='01.05.2020'))

And my desired output should be sth. like:

   col1  col2 col3
0     3     1    3
Jogi
  • 304
  • 4
  • 15
  • What is the issue, exactly? Have you tried anything, done any research? Stack Overflow is not a free code writing service, nor is it meant to provide personalized guides and tutorials. See: [ask], [help/on-topic], https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users. – AMC Mar 16 '20 at 18:45
  • Does this answer your question? [Locate first and last non NaN values in a Pandas DataFrame](https://stackoverflow.com/questions/22403469/locate-first-and-last-non-nan-values-in-a-pandas-dataframe) – AMC Mar 16 '20 at 18:46
  • Since I am relatively new to python, I might have searched for the wrong keywords. However, my question has a reproducible example that should help clarify. I don't need the position, but the value itself. – Jogi Mar 17 '20 at 09:13

1 Answers1

0

Using a apply on a column level, filtering the non-null entries and extracting the last element of each filtered column delivers the result.

df.apply(lambda x: x[x.notnull()].values[-1])
Jogi
  • 304
  • 4
  • 15