pandas dict of dicts to DataFrame
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15455388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dict of dicts of dicts to DataFrame
提问by scls
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 的 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 包含字典而不是值。
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 中的每个 dict 都具有相同的键。
回答by HYRY
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:
如果您不想使用面板:
pd.concat(map(pd.DataFrame, d.itervalues()), keys=d.keys()).stack().unstack(0)

