gpt4 book ai didi

python - 如何从嵌套字典创建扩展的 Pandas 数据框?

转载 作者:行者123 更新时间:2023-12-03 15:34:41 25 4
gpt4 key购买 nike

我有一个嵌套的字典,并试图从中创建一个 Pandas 数据框,但它只提供了两列,我喜欢所有的字典键都是列。
移动电源

import numpy as np
import pandas as pd

history = {'validation_0':
{'error': [0.06725,0.067,0.067],
'error@0.7': [0.104125,0.103875,0.103625],
'auc': [0.92729,0.932045,0.934238],
},
'validation_1':
{'error': [0.1535,0.151,0.1505],
'error@0.7': [0.239,0.239,0.239],
'auc': [0.898305,0.905611,0.909242]
}
}


df = pd.DataFrame(history)
print(df)
validation_0 validation_1
error [0.06725, 0.067, 0.067] [0.1535, 0.151, 0.1505]
error@0.7 [0.104125, 0.103875, 0.103625] [0.239, 0.239, 0.239]
auc [0.92729, 0.932045, 0.934238] [0.898305, 0.905611, 0.909242]
必需的
dataframe with following columns:
validation_0_error validation_1_error validation_0_error@0.7 validation_1_error@0.7 validation_0_auc validation_1_auc

最佳答案

您也可以在 explode 之后对其进行 json_normalize :

print (pd.json_normalize(history).apply(pd.Series.explode).reset_index(drop=True))

validation_0.error validation_0.error@0.7 validation_0.auc validation_1.error validation_1.error@0.7 validation_1.auc
0 0.06725 0.104125 0.92729 0.1535 0.239 0.898305
1 0.067 0.103875 0.932045 0.151 0.239 0.905611
2 0.067 0.103625 0.934238 0.1505 0.239 0.909242

关于python - 如何从嵌套字典创建扩展的 Pandas 数据框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63959574/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com