[英]convert all data within the json to csv using pandas or python
我尝试使用json_normalize展平数据,但部分数据仍未展平。 如何获取此json的拼合数据。 注意:这是一个示例json文件,但嵌套部分中的数据可能会增加。
码:
json_file = {
"data": "abc",
"data2": 123,
"results": {
"name": "w",
"more_data": [
{
"no": "111",
"code": 3
}
],
"id": 1
}
}
data = json_normalize(json_file)
data.to_csv('flatten.csv')
结果:
,data,data2,results.id,results.more_data,results.name
0,abc,123,1,"[{'no': '111', 'code': 3}]",w
results.more_data still gives me a json instead of flattening it.
我如何使它工作?
预期产量:
由于results.more_data是一个数组,因此甚至必须将其展平。 例如:
,data,data2,results.id,results.more_data.0.no,results.more_data.0.code,results.name
0,abc,123,1, '111', "3",w
考虑以下演示:
In [105]: json_file = {
...: "data": "abc",
...: "data2": 123,
...: "results": {
...: "name": "w",
...: "more_data": [
...: {
...: "no": "111",
...: "code": 3
...: },
...: {
...: "no": "222",
...: "code": 4
...: }
...:
...: ],
...: "id": 1
...: }
...: }
...:
In [106]:
In [106]: json_normalize(json_file,
[['results','more_data']],
['data','data2', ['results','id'], ['results','name']],
record_prefix='results.more_data.')
Out[106]:
results.more_data.code results.more_data.no data data2 results.id results.name
0 3 111 abc 123 1 w
1 4 222 abc 123 1 w
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.