简体   繁体   中英

Filter rows based on condition in Pandas

I have dataframe df_groups that contain sample number, group number and accuracy.

Tabel 1: Samples with their groups
    +----+----------+------------+------------+
    |    |   sample |   group    |   Accuracy |
    |----+----------+------------+------------|
    |  0 |        0 |          6 |    91.6    |
    |  1 |        1 |          4 |    92.9333 |
    |  2 |        2 |          2 |    91      |
    |  3 |        3 |          2 |    90.0667 |
    |  4 |        4 |          4 |    91.8    |
    |  5 |        5 |          5 |    92.5667 |
    |  6 |        6 |          6 |    91.1    |
    |  7 |        7 |          5 |    92.3333 |
    |  8 |        8 |          2 |    92.7667 |
    |  9 |        9 |          0 |    91.1333 |
    | 10 |       10 |          4 |    92.5    |
    | 11 |       11 |          5 |    92.4    |
    | 12 |       12 |          7 |    93.1333 |
    | 13 |       13 |          7 |    93.5333 |
    | 14 |       14 |          2 |    92.1    |
    | 15 |       15 |          6 |    93.2    |
    | 16 |       16 |          8 |    92.7333 |
    | 17 |       17 |          8 |    90.8    |
    | 18 |       18 |          3 |    91.9    |
    | 19 |       19 |          3 |    93.3    |
    | 20 |       20 |          5 |    90.6333 |
    | 21 |       21 |          9 |    92.9333 |
    | 22 |       22 |          4 |    93.3333 |
    | 23 |       23 |          9 |    91.5333 |
    | 24 |       24 |          9 |    92.9333 |
    | 25 |       25 |          1 |    92.3    |
    | 26 |       26 |          9 |    92.2333 |
    | 27 |       27 |          6 |    91.9333 |
    | 28 |       28 |          5 |    92.1    |
    | 29 |       29 |          8 |    84.8    |
    +----+----------+------------+------------+

I want to return a dataframe with any accuracy above (eg 92). so the results will be like this

Tabel 1: Samples with their groups when accuracy above 92. 
        +----+----------+------------+------------+
        |    |   sample |   group    |   Accuracy |
        |----+----------+------------+------------|
        |  1 |        1 |          4 |    92.9333 |    
        |  2 |        5 |          5 |    92.5667 |  
        |  3 |        7 |          5 |    92.3333 |
        |  4 |        8 |          2 |    92.7667 |
        | 5  |       10 |          4 |    92.5    |
        | 6  |       11 |          5 |    92.4    |
        | 7  |       12 |          7 |    93.1333 |
        | 8  |       13 |          7 |    93.5333 |
        | 9  |       14 |          2 |    92.1    |
        | 10 |       15 |          6 |    93.2    |
        | 11 |       16 |          8 |    92.7333 |   
        | 12 |       19 |          3 |    93.3    |
        | 13 |       21 |          9 |    92.9333 |
        | 14 |       22 |          4 |    93.3333 |
        | 15 |       24 |          9 |    92.9333 |
        | 16 |       25 |          1 |    92.3    |
        | 17 |       26 |          9 |    92.2333 |
        | 18 |       28 |          5 |    92.1    | 
        +----+----------+------------+------------+

so, the result will return based on the condition that is greater than or equal to the predefined accuracy (eg 92, 90 or 85, ect).

You can use df.loc[df['Accuracy'] >= predefined_accuracy] .

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