【问题标题】:How to filter data with pandas dataframe [duplicate]如何使用熊猫数据框过滤数据[重复]
【发布时间】:2021-04-05 23:31:39
【问题描述】:

我希望过滤 Pandas 数据框以从两种不同类型的属性中选择数据:

restaurants=df['restaurants'][df['restaurants']=='Español' or df['restaurants']=='Italiano']

但是我不能使用这个选项,或者你能给我一个例子来说明如何做到这一点。

【问题讨论】:

  • 使用:df[df['restaurants']=='Español' | df['restaurants']=='Italiano']

标签: pandas dataframe


【解决方案1】:
restaurants=df[(df['restaurants']=='Español') | (df['restaurants']=='Italiano')] 

【讨论】:

    【解决方案2】:
    # method1
    rest_list = ['Español', 'Italiano']
    cond = df['restaurants'].isin(rest_list)
    restaurants = df.loc[cond,'restaurants']
    
    # method2
    cond = False
    cond |= df['restaurants']=='Español'
    cond |= df['restaurants']=='Italiano'
    restaurants = df.loc[cond,'restaurants']
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-25
      • 2017-05-10
      • 1970-01-01
      • 1970-01-01
      • 2017-05-18
      • 1970-01-01
      • 2021-12-30
      • 2019-04-13
      相关资源
      最近更新 更多