我想将国家/地区列表与列数据进行比较,列数据是Pandas数据框Python中的字典对象类型

问题描述 投票:1回答:1
list = ['Japan', 'France', 'United States']

要比较具有列名place键'country'和值''的列表仅获取具有与列表中的国家/地区相似的国家/地区的行

数据框看起来像这样:

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个回答
0
投票

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

Series.isin

Series.isin
© www.soinside.com 2019 - 2024. All rights reserved.