Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas: select first and last valid floats in columns

Tags:

python

pandas

With a DataFrame that looks like this:

     tra98  tra99  tra100  tra101  tra102
0   0.1880  0.345  0.1980  0.2090  0.2190
1   0.2510  0.585  0.2710  0.3240  0.2920
2   0.3240  0.741  0.2190  0.2090  0.2820
3   0.2820  0.825  0.1040  0.1880  0.2400
4   0.2190  1.150  0.0940  0.1360  0.1770
5   0.2300  1.210  0.0522  0.0209  0.0731
6   0.1670  1.290  0.0626  0.0104  0.0104
7   0.0835  1.400  0.0104     NaN     NaN
8   0.0418  1.580     NaN     NaN     NaN
9   0.0209    NaN     NaN     NaN     NaN
10     NaN    NaN     NaN     NaN     NaN
11     NaN    NaN     NaN     NaN     NaN
12     NaN    NaN     NaN     NaN     NaN

How can I select the first and last valid values in each column?

Thank you for your help.

like image 574
t_n Avatar asked Nov 20 '25 00:11

t_n


1 Answers

The following shows how you can iterate over the cols, then call dropna() and then access the first and last values calling iloc:

In [21]:

for col in df:
    valid_col = df[col].dropna()
    print("column:", col, "  first:", valid_col.iloc[0], "  last:", valid_col.iloc[-1])

column: tra98   first: 0.188   last: 0.0209
column: tra99   first: 0.345   last: 1.58
column: tra100   first: 0.198   last: 0.0104
column: tra101   first: 0.209   last: 0.0104
column: tra102   first: 0.219   last: 0.0104
like image 55
EdChum Avatar answered Nov 22 '25 15:11

EdChum