我有一个嵌套的字典,并试图以此创建一个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
答案 0 :(得分:6)
您也可以在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
答案 1 :(得分:5)
让我们尝试一下:
a = df.unstack()
pd.DataFrame(a.values.tolist(), index=a.index).T
如果您从history
开始,
pd.concat({k:pd.DataFrame(v) for k,v in history.items()}, axis=1)
输出:
validation_0 validation_1
error error@0.7 auc error error@0.7 auc
0 0.06725 0.104125 0.927290 0.1535 0.239 0.898305
1 0.06700 0.103875 0.932045 0.1510 0.239 0.905611
2 0.06700 0.103625 0.934238 0.1505 0.239 0.909242