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


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)

使用,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'}

最新更新