簡體   English   中英

根據最大列值過濾數據框行

[英]Filter dataframe rows according to max column value


我有這個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

給定這些用戶輸入:

input_value = 0.8
input_class = 'class2'

我使用以下代碼根據class2最大值對數據幀重新排序:

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

像這樣:

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

但是,我仍然需要滿足一個類條件,即class2值必須是每一行中的最高float值。 換一種說法:

0   Portishead               Roads   0.98    0.02          0.0

應該討論,因為最大值屬於另一個類。

如何在上面的代碼段中插入此條件?

找到沿列的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