python-3.x - 我想将国家列表与列数据进行比较,列数据是 pandas 数据框 Python 中的字典对象类型

标签 python-3.x pandas list dictionary compare

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/

相关文章:

python - 打印在 __getattr__ 中不起作用

python - Pandas 字符串方法来处理小数位

list - Prolog:具有元素总和 S 的 k 个元素的排列

python - python中搜索文本正文中多个项目的最快方法

java - 如何判断java中空列表的内容类型

python - 遍历多维列表

python - 删除具有csv文件某些关键字的行

python - 用于比较多个数据列的循环 T 检验

python - 取 Pandas Column 的前 6 位数字

python - 使用日期逻辑创建假数据