简体   繁体   中英

Get last non-null value of a row and its column in pandas DataFrame

I want to get the last non-null value (rightmost) of row C in this DataFrame. With that, I also want to get its Year (column name).

Here is my DataFrame :

df = pd.DataFrame(np.random.randint(0,100,size=(4, 5)),
                  columns=['2016', '2017', '2018', '2019', '2020'],
                  index=['A', 'B', 'C', 'D'])
df.iloc[2, 2:5] = np.NaN
print(df)
    2016    2017    2018    2019    2020
A   41      69      63.0    85.0    16.0
B   12      99      88.0    87.0    13.0
C   80      15      NaN     NaN     NaN
D   42      27      3.0     76.0    6.0

Result should look like {'year' : 2017, 'value' : 15} . What's the best way of achieving that result ?

Something like this should solve it

In [1]: import pandas as pd 
   ...: import numpy as np 
   ...: df = pd.DataFrame(np.random.randint(0,100,size=(4, 5)), 
   ...:                   columns=['2016', '2017', '2018', '2019', '2020'], 
   ...:                   index=['A', 'B', 'C', 'D']) 
   ...: df.iloc[2, 2:5] = np.NaN 
   ...: print(df)                                                                                                                                                                                     
   2016  2017  2018  2019  2020
A    13    78   9.0  13.0  98.0
B    35     3  32.0   6.0  42.0
C    26    24   NaN   NaN   NaN
D    77    91  96.0  60.0  94.0

In [2]: value = int(df.loc['C'][~df.loc['C'].isna()][-1])                                                                                                                                             

In [3]: year = df.loc['C'][df.loc['C'] == value].index.values[0]                                                                                                                                      

In [4]: result = {'year': year, 'value': value}                                                                                                                                                       

In [5]: result                                                                                                                                                                                        
Out[5]: {'year': '2017', 'value': 24}

You can break the expressions above part by part to better understand how each functionality is getting used together here to yield the desired output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM