【发布时间】:2018-06-09 06:43:33
【问题描述】:
这是样本数据集
datetime 2017-12-24 2017-12-31
a 1.5 0.5
b 0.0 1.5
c 1.5 0.0
我正在使用 to_json 将其转换为 json 并得到此输出
{"2017-12-24":{"a":1.5,"b":0.0,"c":1.5},"2017-12-31":{"a":0.5,"b":1.5,"c":0.0}}
我想要做的是让 json 像这样的 dict 列表
[{"datetime":"2017-12-24","a":1.5,"b":0.0,"c":1.5},{"datetime":"2017-12-31", "a":0.5,"b":1.5,"c":0.0}]
我的代码在这里
df = pd.DataFrame({'2017-12-24': [1.5, 0.0, 1.5], '2017-12-31': [0.5, 1.5, 0.0]}, index=['a', 'b', 'c'])
df.columns.name='datetime'
print(df.to_json())
【问题讨论】:
标签: python json python-3.x pandas dataframe