list = ['Japan', 'France', 'United States']
想要比较列表与列名 place key 'country' and value '' 只获取那些国家与列表中的国家相似的行
数据框看起来像这样:
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.get
连同 Series.isin
创建一个 bool 掩码然后使用这个掩码过滤行:
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'}
关于python-3.x - 我想将国家列表与列数据进行比较,列数据是 pandas 数据框 Python 中的字典对象类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62359926/