【问题标题】:I want to compare country list with column data which is dictionary object type in pandas dataframe Python我想将国家/地区列表与作为熊猫数据框 Python 中字典对象类型的列数据进行比较
【发布时间】:2020-10-03 04:40:30
【问题描述】:
list = ['Japan', 'France', 'United States']

想要比较列表与列名地点键“国家”和值“” 仅获取与列表中的国家/地区相似的那些行

数据框如下所示:

Id        Place
767       {'country_code': 'US','country': 'United States'}
645       {'country_code': 'IRL','country': 'Ireland'}
324       {'country_code': 'JAP','country': 'Japan'}

我用过这个:

 for i in range(0,len(df['place'])):
      df['place'][0]["country"].isin(list)

【问题讨论】:

    标签: python-3.x pandas list dictionary compare


    【解决方案1】:

    使用Series.str.getSeries.isin 创建一个布尔掩码,然后使用此掩码过滤行:

    m = df['Place'].str.get('country').isin(lst)
    df = df[m]
    

    # print(df)
        Id                                              Place
    0  767  {'country_code': 'US', 'country': 'United States'}
    2  324        {'country_code': 'JAP', 'country': 'Japan'}
    

    【讨论】:

    • 谢谢shubham sharma,它可以按我的需要正常工作。这是我在 stackoverflow 上的第一个问题,并在几分钟内得到了回答。
    猜你喜欢
    • 1970-01-01
    • 2022-01-06
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    • 2021-08-16
    • 1970-01-01
    • 2017-01-31
    • 2020-11-06
    相关资源
    最近更新 更多