Home > Mobile >  Slice dataframe based on last row valuse
Slice dataframe based on last row valuse

Time:10-27

I would like to slice a dataframe so, that I output only columns where values in the last row (or at a certain index) are bigger than a certain criterion. Example:

The dataframe:

Name               T        TU      TUXT      TEXT      TEST
Date                                                  
2022-10-02  0.207906  0.211786  0.145491  0.125727  0.230134
2022-10-09  0.196817  0.208811  0.149292  0.112480  0.213108
2022-10-16  0.228247  0.236007  0.154773  0.126083  0.247899
2022-10-23  0.303633  0.308214  0.231662  0.168093  0.317019
2022-10-30  0.100477  0.099930  0.075825  0.048684  0.104480

I would like to output only columns where values on 2022-10-30 are greater than 0.1:

Name               T      TEST
Date                               
2022-10-02  0.207906  0.230134
2022-10-09  0.196817  0.213108
2022-10-16  0.228247  0.247899
2022-10-23  0.303633  0.317019
2022-10-30  0.100477  0.104480

Thanks in advance!

CodePudding user response:

Use DataFrame.loc for select rows by labels and then filter, first : means get all rows and columns by mask:

df1 = df.loc[:, df.loc['2022-10-30'].gt(0.1)]
print (df1)
                   T      TEST
Date                          
2022-10-02  0.207906  0.230134
2022-10-09  0.196817  0.213108
2022-10-16  0.228247  0.247899
2022-10-23  0.303633  0.317019
2022-10-30  0.100477  0.104480

Or if need filter by last row use DataFrame.iloc:

df2 = df.loc[:, df.iloc[-1].gt(0.1)]
  • Related