create a new dataframe from selecting specific rows from existing dataframe python

前端 未结 1 1547
北荒
北荒 2021-02-05 22:13

i have a table in my pandas dataframe. df

id count price
1    2     100
2    7      25
3    3     720
4    7     221
5    8     212
6    2     200
相关标签:
1条回答
  • You nedd add () because & has higher precedence than ==:

    df3 = df[(df['count'] == '2') & (df['price'] == '100')]
    print (df3)
      id count price
    0  1     2   100
    

    If need check multiple values use isin:

    df4 = df[(df['count'].isin(['2','7'])) & (df['price'].isin(['100', '221']))]
    print (df4)
      id count price
    0  1     2   100
    3  4     7   221
    

    But if check numeric, use:

    df3 = df[(df['count'] == 2) & (df['price'] == 100)]
    print (df3)
    
    df4 = df[(df['count'].isin([2,7])) & (df['price'].isin([100, 221]))]
    print (df4)
    
    0 讨论(0)
提交回复
热议问题