2 回答

TA贡献1836条经验 获得超3个赞
这样的事情会起作用(您的输入列表称为l):
tempdf = pd.DataFrame(l)
这产生
complete mid time volume
0 True {'o': '6.07260', 'h': '6.07260', 'l': '6.07260... 2004-05-31T21:00:00.000000000Z 2
1 True {'o': '6.08790', 'h': '6.08790', 'l': '6.08790... 2004-06-01T21:00:00.000000000Z 2
现在您可以将字典mid分成几列并使用concat:
df = pd.concat([tempdf.drop('mid', axis=1), tempdf['mid'].apply(pd.Series)], axis=1)
这给出了预期的结果:
complete time volume o h l c
0 True 2004-05-31T21:00:00.000000000Z 2 6.07260 6.07260 6.07260 6.07260
1 True 2004-06-01T21:00:00.000000000Z 2 6.08790 6.08790 6.08790 6.08790

TA贡献1811条经验 获得超4个赞
使用 pandas 函数会更快更简单。例如,它可以是很好的起点。如果有必要,我添加了重命名索引和枢轴以制作关闭、最高、最低、打开列:
import pandas as pd
d = {'complete': True,
'volume': 2,
'time': '2004-05-31T21:00:00.000000000Z',
'mid': {'o': '6.07260', 'h': '6.07260', 'l': '6.07260', 'c': '6.07260'}}
df = pd.DataFrame(d)
df.rename(index={'o': 'Open', 'h': 'High', 'l': 'Low', 'c': 'Close'}, inplace=True)
df['column'] = df.index
df = pd.pivot_table(df, columns=['column'], index=['complete', 'time', 'volume'], values=['mid'], aggfunc=np.sum)
pd.set_option('display.max_columns', 1000)
添加回答
举报