alex_bn_lee

导航

[985] Filter by Column Value & Multiple Conditions in Pandas dataframe

ref: Ways to filter Pandas DataFrame by column values


  1. Filter by Column Value:

    • To select rows based on a specific column value, use the index chain method. For example, to filter rows where sales are over 300:
      Python
      greater_than = df[df['Sales'] > 300]
    • This will return rows with sales greater than 300.
  2. Filter by Multiple Conditions:

    • Use the & (and) or | (or) operators to filter based on multiple conditions. For instance:
      Python
      and_operator = df[(df['Sales'] > 300) & (df['Units'] > 20)]
      or_operator = df[(df['Sales'] > 300) | (df['Units'] > 20)]
    • and_operator filters rows where sales are greater than 300 and units are greater than 20.
    • or_operator filters rows where either sales or units exceed the specified thresholds.
 

posted on 2024-04-23 08:34  McDelfino  阅读(4)  评论(0编辑  收藏  举报