Get first row of dataframe in Python Pandas based on criteria

前端 未结 4 1561
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-02 20:29

Let\'s say that I have a dataframe like this one

import pandas as pd
df = pd.DataFrame([[1, 2, 1], [1, 3, 2], [4, 6, 3], [4, 3, 4], [5, 4, 5]], columns=[\'A\         


        
4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-02 20:38

    For existing matches, use query:

    df.query(' A > 3' ).head(1)
    Out[33]: 
       A  B  C
    2  4  6  3
    
    df.query(' A > 4 and B > 3' ).head(1)
    Out[34]: 
       A  B  C
    4  5  4  5
    
    df.query(' A > 3 and (B > 3 or C > 2)' ).head(1)
    Out[35]: 
       A  B  C
    2  4  6  3
    

提交回复
热议问题