python - 根据表列中 JSON 中的 id 选择表的行

标签 python json pandas dataframe

我需要根据 id 在 JSON 中使用 Pandas 在其中一列中选择表的行。

例子:

<表类="s-表"> <头> column_a column_b column_c <正文> aaaa bbbbb {'id' : cc, 'name' : xx ...} xxxx yyyy {'id' : ff, 'name' : gg ...}

所以我想选择 column_c 中 JSON 的 id 等于 'cc' 的所有行,所以结果将是:

<表类="s-表"> <头> column_a column_b column_c <正文> aaaa bbbbb {'id' : cc, 'name' : xx ...}

最佳答案

试试这个:

import json

def func(x): #x is a string of json format
    s=json.loads(x)
    return s['id']=='cc'

并通过将上述函数应用于 column_c 来过滤数据框:

df[df['column_c'].apply(lambda x: func(x)]

如果这不起作用,则意味着 column_c 是不同的类型。那样的话,请提供这个列的类型,以便我调整解决方案

关于python - 根据表列中 JSON 中的 id 选择表的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65361807/

相关文章:

python - Horovod 和 Tensorflow 估计器

javascript - 允许用户通过 json 对象配置设置

python - 将数据框中的负值转换为正值 - Python

python - 为什么 rstrip 不修改字符串列表

python - 将 2 个 python 类对象添加在一起

python - 无法从 plt.bar 生成条形图

python - 如何将列表与 python 中的另一个列表进行匹配

r - 使用 key 连接到 R 中的 Rest API

python - 如何将行添加到数据框中以组织时间序列

python - 你如何使用 pandas.DataFrame 列作为索引、列和值?