Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 根据表列中JSON中的id选择表的行_Python_Json_Pandas_Dataframe - Fatal编程技术网

Python 根据表列中JSON中的id选择表的行

Python 根据表列中JSON中的id选择表的行,python,json,pandas,dataframe,Python,Json,Pandas,Dataframe,我需要使用Pandas根据JSON中的id在其中一列中选择表的行 例如: a列 b列 c列 aaaa bbbbb {'id':cc,'name':xx…} xxxx 年份 {'id':ff,'name':gg…} 如果您已经将其加载到pandas中,那么它现在可能是一个字典,可以通过类似于dataframe的键访问该字典,因此您希望像这样进行过滤: df[df['column_c']['id'] == 'cc'] 试试这个: import json def func(x): #x is a

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

例如:

a列 b列 c列 aaaa bbbbb {'id':cc,'name':xx…} xxxx 年份 {'id':ff,'name':gg…}
如果您已经将其加载到pandas中,那么它现在可能是一个字典,可以通过类似于dataframe的键访问该字典,因此您希望像这样进行过滤:

df[df['column_c']['id'] == 'cc']
试试这个:

import json

def func(x): #x is a string of json format
    s=json.loads(x)
    return s['id']=='cc'
并通过将上述函数应用于列_c来过滤数据帧:

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

如果这不起作用,则表示列_c是另一种类型。在这种情况下,请提供此列的类型,以便我调整解决方案

这将不适用于列