简体   繁体   中英

Filter dataframe rows according to max column value


I have this df dataframe:

        artist               track  class1  class2      class3
0   Portishead               Roads   0.98    0.02          0.0
1  Yo La Tengo     Our Way to Fall   0.14    0.86          0.0
2    Radiohead  Fake Plastic Trees   0.03    0.97          0.0

given these user inputs:

input_value = 0.8
input_class = 'class2'

I use the following code in reorder the dataframe according to class2 max value:

 for col in df.ix[:,'class1':'class3']:
     if col == input_class:
        reordered_df = df.iloc[(df[input_class] - input_value).argsort()]

like so:

1  Yo La Tengo     Our Way to Fall   0.14    0.86          0.0
2    Radiohead  Fake Plastic Trees   0.03    0.97          0.0
0   Portishead               Roads   0.98    0.02          0.0

however, I still need to satisfy one class condition , that is class2 value must be the highest float value in each row . in other words:

0   Portishead               Roads   0.98    0.02          0.0

should be discarted, because max value belongs to another class.

how do I insert this condition on the snippet above?

Find the max row-wise along the columns, compare to class2 , and discard accordingly.

reordered_df
        artist               track  class1  class2  class3
1  Yo La Tengo     Our Way to Fall    0.14    0.86     0.0
2    Radiohead  Fake Plastic Trees    0.03    0.97     0.0
0   Portishead               Roads    0.98    0.02     0.0

reordered_df[reordered_df.max(1) == reordered_df.class2]
        artist               track  class1  class2  class3
1  Yo La Tengo     Our Way to Fall    0.14    0.86     0.0
2    Radiohead  Fake Plastic Trees    0.03    0.97     0.0

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