简体   繁体   English

根据最大列值过滤数据框行

[英]Filter dataframe rows according to max column value


I have this df dataframe: 我有这个df数据框:

        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: 我使用以下代码根据class2最大值对数据帧重新排序:

 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 . 但是,我仍然需要满足一个类条件,即class2值必须是每一行中的最高float值。 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. 找到沿列的max行,与class2比较,并相应地丢弃。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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