繁体   English   中英

使用python3将json数据转换为csv但它不起作用

[英]Convert json data to csv with python3 but it doesn't work

我有一个包含一组数字的大数据集

数据是从服务器获取的

每行数据是这样的:

{"body_data":[
{'height': 170.00, 'weight': 165.00, 'blood': 3.00, 'sugar': 100.02, 'fat': 36.02, 'time_object': 1544443260000},
{'height': 170.00, 'weight': 165.00, 'blood': 3.00, 'sugar': 100.02, 'fat': 36.02, 'time_object': 1544443260000},
],"symbol":"DATA_FAT","empty":false}

我试图将数据保存为 *.json 格式,将其作为新文件导入并用 csv 重写,但出现错误。

我用熊猫尝试了以下代码:

df = pd.DataFrame.from_dict(data, orient='index',columns=['open', 'height', 'weight', 'blood', 'sugar', 'fat', 'time_object'])

它给了我以下错误:

 File "pandas/_libs/src/inference.pyx", line 1517, in pandas._libs.lib.to_object_array
TypeError: object of type 'bool' has no len()

谁能帮帮我

我相信您需要选择嵌套键body_data

df = pd.DataFrame(data['body_data'])
print (df)
   blood    fat  height   sugar    time_object  weight
0    3.0  36.02   170.0  100.02  1544443260000   165.0
1    3.0  36.02   170.0  100.02  1544443260000   165.0

如果想要更改列的顺序( open键不在示例数据中,因此输出中为NaN ):

df = pd.DataFrame(data['body_data'],
                  columns=['open', 'height', 'weight', 'blood', 'sugar', 'fat', 'time_object'])
print (df)
   open  height  weight  blood   sugar    fat    time_object
0   NaN   170.0   165.0    3.0  100.02  36.02  1544443260000
1   NaN   170.0   165.0    3.0  100.02  36.02  1544443260000

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM