I'd like to store JSON data in a Python Pandas DataFrame
我想将JSON数据存储在Python Pandas DataFrame中
my JSON data is a dict of dicts of dicts like this
我的JSON数据是像这样的dicts的词典
d = {
"col1": {
"row1": {
"data1": "0.87",
"data2": "Title col1",
"data3": "14.4878",
"data4": "Title row1"
},
"row2": {
"data1": "15352.3",
"data2": "Title col1",
"data3": "14.9561",
"data4": "Title row2"
},
"row3": {
"data1": "0",
"data2": "Title col1",
"data3": "16.8293",
"data4": "Title row3"
}
},
"col2": {
"row1": {
"data1": "0.87",
"data2": "Title col2",
"data3": "24.4878",
"data4": "Title row1"
},
"row2": {
"data1": "15352.3",
"data2": "Title col2",
"data3": "24.9561",
"data4": "Title row2"
},
"row3": {
"data1": "0",
"data2": "Title col2",
"data3": "26.8293",
"data4": "Title row3"
}
}
}
I did this to put my data in a DataFrame
我这样做是为了将我的数据放在DataFrame中
import pandas as pd
df=pd.DataFrame(d)
I get this
我明白了
In [1]: df
Out[1]:
col1 col2
row1 {'data4': 'Title col1', 'data1': '0.87', 'data3': {'data4': 'Title col1', 'data1': '0.87', 'data3':
row2 {'data4': 'Title col2', 'data1': '15352.3', 'data {'data4': 'Title col2', 'data1': '15352.3', 'data
row3 {'data4': 'Title col3', 'data1': '0', 'data3': '1 {'data4': 'Title col3', 'data1': '0', 'data3': '2
My problem is that my DataFrame contains dicts instead of values.
我的问题是我的DataFrame包含dicts而不是值。
I wonder how I can manage multidimensionnal data (more than 2 dimensions... 3 dimensions here) with a Pandas DataFrame.
我想知道如何使用Pandas DataFrame管理多维数据(超过2维... 3维)。
Each dict inside DataFrame have the same keys.
DataFrame中的每个字典都有相同的键。
1 个解决方案
#1
17
df = pd.Panel.from_dict(d).to_frame()
output:
输出:
col1 col2
major minor
data1 row1 0.87 0.87
row2 15352.3 15352.3
row3 0 0
data2 row1 Title col1 Title col2
row2 Title col1 Title col2
row3 Title col1 Title col2
data3 row1 14.4878 24.4878
row2 14.9561 24.9561
row3 16.8293 26.8293
data4 row1 Title row1 Title row1
row2 Title row2 Title row2
row3 Title row3 Title row3
If you don't want use Panel:
如果您不想使用Panel:
pd.concat(map(pd.DataFrame, d.itervalues()), keys=d.keys()).stack().unstack(0)
#1
17
df = pd.Panel.from_dict(d).to_frame()
output:
输出:
col1 col2
major minor
data1 row1 0.87 0.87
row2 15352.3 15352.3
row3 0 0
data2 row1 Title col1 Title col2
row2 Title col1 Title col2
row3 Title col1 Title col2
data3 row1 14.4878 24.4878
row2 14.9561 24.9561
row3 16.8293 26.8293
data4 row1 Title row1 Title row1
row2 Title row2 Title row2
row3 Title row3 Title row3
If you don't want use Panel:
如果您不想使用Panel:
pd.concat(map(pd.DataFrame, d.itervalues()), keys=d.keys()).stack().unstack(0)