如何使用Python或Pandas (最好)将Pandas DataFrame转换为列表字典,以便输入到高级图表中?
最接近我的是:
df.T.to_json('bar.json', orient='index')但是,这是一份一分为二,而不是一份清单。
我的意见:
import pandas
import numpy as np
df = pandas.DataFrame({
"date": ['2014-10-1', '2014-10-2', '2014-10-3', '2014-10-4', '2014-10-5'],
"time": [1, 2, 3, 4, 5],
"temp": np.random.random_integers(0, 10, 5),
"foo": np.random.random_integers(0, 10, 5)
})
df2 = df.set_index(['date'])
df2输出:
time temp foo
date
2014-10-1 1 3 0
2014-10-2 2 8 7
2014-10-3 3 4 9
2014-10-4 4 4 8
2014-10-5 5 6 2期望输出:我在高级图表中使用此输出,这要求它成为如下列表的字典:
{'date': ['2014-10-1', '2014-10-2', '2014-10-3', '2014-10-4', '2014-10-5'],
'foo': [7, 2, 5, 5, 6],
'temp': [8, 6, 10, 10, 3],
'time': [1, 2, 3, 4, 5]}发布于 2014-11-17 03:39:53
In [199]: df2.reset_index().to_dict(orient='list')
Out[199]:
{'date': ['2014-10-1', '2014-10-2', '2014-10-3', '2014-10-4', '2014-10-5'],
'foo': [8, 1, 8, 8, 1],
'temp': [10, 10, 8, 3, 10],
'time': [1, 2, 3, 4, 5]}发布于 2018-08-03 18:22:16
创建每行字典的列表
post_data_list = []
for i in df2.index:
data_dict = {}
for column in df2.columns:
data_dict[column] = df2[column][i]
post_data_list.append(data_dict)https://stackoverflow.com/questions/26964993
复制相似问题