我的知识不足以做到这一点:(我从看起来像这样的数据开始:
{"data": {"1547700225.29": -5.3369832056203785, "1547700227.54": -6.044502239243294, "1547700229.7899997": -4.642320938763093, "1547700231.9799995": -5.104047573562501, "1547700234.1799998": -5.104047573562501, "1547700236.4699998": -5.3369832056203785, "1547700238.7699995": -5.807173592599741}}
到目前为止,这是我的脚本:
import csv
import json
import pandas as pd
from pandas.io.json import json_normalize
def json_csv():
with open('metis.json') as data_file:
data=json.load(data_file)
normalized_df = pd.io.json.json_normalize(data)
normalized_df = pd.DataFrame.transpose(normalized_df)
normalized_df.to_csv('metis.csv',index=False)
return
def main():
json_csv()
main()
我需要得到一个如下所示的CSV文件:
Time, Value
1547700225.29,-5.3369832056203785
1547700227.54,-6.044502239243294
1547700229.7899997,-4.642320938763093
1547700231.9799995,-5.104047573562501
1547700234.1799998,-5.104047573562501
1547700236.4699998,-5.3369832056203785
1547700238.7699995,-5.807173592599741
当我以交互方式运行命令时,转置起作用,因此它与数据结构和/或to_csv方法有关。我只是不知道该怎么做。
此外,时间也是新纪元,我需要将其转换为人类可以理解的日期时间。
有什么建议吗?我不介意研究是否朝着正确的方向发展,但我已经读了很多书,并且找不到正确的术语来找到答案。
答案 0 :(得分:0)
使用:
df = pd.read_json('metis.json').reset_index()
df.columns = ['Time', 'Value']
df.to_csv('output.csv')
# Also has human readable time
+---+-------------------------+-----------+
| | Time | Value |
+---+-------------------------+-----------+
| 0 | 2019-01-17 04:43:45.290 | -5.336983 |
| 1 | 2019-01-17 04:43:47.540 | -6.044502 |
| 2 | 2019-01-17 04:43:49.790 | -4.642321 |
| 3 | 2019-01-17 04:43:51.980 | -5.104048 |
| 4 | 2019-01-17 04:43:54.180 | -5.104048 |
| 5 | 2019-01-17 04:43:56.470 | -5.336983 |
| 6 | 2019-01-17 04:43:58.770 | -5.807174 |
+---+-------------------------+-----------+