selecting rows based on multiple column values in pandas dataframe

前端 未结 4 713
梦如初夏
梦如初夏 2021-02-12 09:41

I have a pandas DataFrame df:

import pandas as pd

data = {\"Name\": [\"AAAA\", \"BBBB\"],
        \"C1\": [25, 12],
              


        
相关标签:
4条回答
  • Shorter version:

    In [65]:
    
    df[(df>=0)&(df<=20)].dropna()
    Out[65]:
       Name  C1  C2  C3
    1  BBBB  12   1  10
    
    0 讨论(0)
  • 2021-02-12 10:17
    df.query(
        "0 < C1 < 20 and 0 < C2 < 20 and 0 < C3 < 20"
    )
    

    or

    df.query("0 < @df < 20").dropna()
    

    Using @foo in df.query refers to the variable foo in the environment.

    0 讨论(0)
  • 2021-02-12 10:20

    I think below should do it, but its elegance is up for debate.

    new_df = old_df[((old_df['C1'] > 0) & (old_df['C1'] < 20)) & ((old_df['C2'] > 0) & (old_df['C2'] < 20)) & ((old_df['C3'] > 0) & (old_df['C3'] < 20))]
    
    0 讨论(0)
  • 2021-02-12 10:29

    I like to use df.query() for these kind of things

    df.query('C1>=0 and C1<=20 and C2>=0 and C2<=20 and C3>=0 and C3<=20')
    
    0 讨论(0)
提交回复
热议问题