pandas Python:json_normalize 熊猫系列给出了 TypeError
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45325208/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python: json_normalize a pandas series gives TypeError
提问by zufanka
I have tens of thousands rows of json snippets like this in a pandas series df["json"]
我在 Pandas 系列中有数万行这样的 json 片段 df["json"]
[{
'IDs': [{
'lotId': '1',
'Id': '123456'
}],
'date': '2009-04-17',
'bidsCount': 2,
}, {
'IDs': [{
'lotId': '2',
'Id': '123456'
}],
'date': '2009-04-17',
'bidsCount': 4,
}, {
'IDs': [{
'lotId': '3',
'Id': '123456'
}],
'date': '2009-04-17',
'bidsCount': 8,
}]
Sample of the original file:
原始文件示例:
{"type": "OPEN","title": "rainbow","json": [{"IDs": [{"lotId": "1","Id": "123456"}],"date": "2009-04-17","bidsCount": 2,}, {"IDs": [{"lotId": "2","Id": "123456"}],"date": "2009-04-17","bidsCount": 4,}, {"IDs": [{"lotId": "3","Id": "123456"}],"date": "2009-04-17","bidsCount": 8,}]}
{"type": "CLOSED","title": "clouds","json": [{"IDs": [{"lotId": "1","Id": "23345"}],"date": "2009-05-17","bidsCount": 2,}, {"IDs": [{"lotId": "2","Id": "23345"}],"date": "2009-05-17","bidsCount": 4,}, {"IDs": [{"lotId": "3","Id": "23345"}],"date": "2009-05-17","bidsCount": 8,}]}
df = pd.read_json("file.json", lines=True)
I am trying to make them into a data frame, something like
我试图把它们变成一个数据框,比如
Id lotId bidsCount date
123456 1 2 2009-04-17
123456 2 4 2009-04-17
123456 3 8 2009-04-17
by using
通过使用
json_normalize(df["json"])
json_normalize(df["json"])
However I get
但是我得到
AttributeError: 'list' object has no attribute 'values'
AttributeError: 'list' object has no attribute 'values'
I guess the json snippet is seen as a list, however I can not figure out how to make it work otherwise. Help appreciated!
我想 json 片段被视为一个列表,但是我无法弄清楚如何使其工作。帮助表示赞赏!
回答by Bharath
I think your df['json']
is a nested list. You can use a for loop and concatenate the dataframe to get the big dataframe i.e
我认为你df['json']
是一个嵌套列表。您可以使用 for 循环并连接数据帧以获得大数据帧,即
Data:
数据:
{"type": "OPEN","title": "rainbow","json": [{"IDs": [{"lotId": "1","Id": "123456"}],"date": "2009-04-17","bidsCount": 2,}, {"IDs": [{"lotId": "2","Id": "123456"}],"date": "2009-04-17","bidsCount": 4,}, {"IDs": [{"lotId": "3","Id": "123456"}],"date": "2009-04-17","bidsCount": 8,}]}
{"type": "CLOSED","title": "clouds","json": [{"IDs": [{"lotId": "1","Id": "23345"}],"date": "2009-05-17","bidsCount": 2,}, {"IDs": [{"lotId": "2","Id": "23345"}],"date": "2009-05-17","bidsCount": 4,}, {"IDs": [{"lotId": "3","Id": "23345"}],"date": "2009-05-17","bidsCount": 8,}]}
df = pd.read_json("file.json", lines=True)
DataFrame:
数据框:
new_df = pd.concat([pd.DataFrame(json_normalize(x)) for x in df['json']],ignore_index=True)
Output:
输出:
IDs bidsCount date 0 [{'Id': '123456', 'lotId': '1'}] 2 2009-04-17 1 [{'Id': '123456', 'lotId': '2'}] 4 2009-04-17 2 [{'Id': '123456', 'lotId': '3'}] 8 2009-04-17 3 [{'Id': '23345', 'lotId': '1'}] 2 2009-05-17 4 [{'Id': '23345', 'lotId': '2'}] 4 2009-05-17 5 [{'Id': '23345', 'lotId': '3'}] 8 2009-05-17
If you want the keys of IDs as columns then you use
如果您希望 ID 的键作为列,那么您可以使用
new_df['lotId'] = [x[0]['lotId'] for x in new_df['IDs']]
new_df['IDs'] = [x[0]['Id'] for x in new_df['IDs']]
IDs bidsCount date lotId 0 123456 2 2009-04-17 1 1 123456 4 2009-04-17 2 2 123456 8 2009-04-17 3 3 23345 2 2009-05-17 1 4 23345 4 2009-05-17 2 5 23345 8 2009-05-17 3