我有一个json记录列表,可以读入pandas。
import pandas as pd
data = [{'eurusd': {'buy': 1, 'sell': 2}, 'eurcny': {'buy': 3, 'sell': 4}},
{'eurusd': {'buy': 5, 'sell': 6}, 'eurcny': {'buy': 7, 'sell': 8}}]
>>> pd.DataFrame(data[0])
eurcny eurusd
buy 3 1
sell 4 2
我想知道是否可以做类似的事情:
pd.Panel(data,items = ['buy','sell'],major_axis = [0,1],minor_axis = ['eurusd','eurcny'])
答案 0 :(得分:0)
Panel接受DataFrames的dict,所以你可以传递它:
In [11]: pd.Panel(dict(enumerate([pd.DataFrame(x) for x in data])))
Out[11]:
<class 'pandas.core.panel.Panel'>
Dimensions: 2 (items) x 2 (major_axis) x 2 (minor_axis)
Items axis: 0 to 1
Major_axis axis: buy to sell
Minor_axis axis: eurcny to eurusd
In [12]: pd.Panel(dict(enumerate([pd.DataFrame(x) for x in data])), items=['buy', 'sell'], major_axis=[0, 1], minor_axis=['eurusd', 'eurcny'])
Out[12]:
<class 'pandas.core.panel.Panel'>
Dimensions: 2 (items) x 2 (major_axis) x 2 (minor_axis)
Items axis: buy to sell
Major_axis axis: 0 to 1
Minor_axis axis: eurusd to eurcny