问题描述
我有这个数据集,我想将其导出为excel文件(如图所示),分为两张表(“ Fabrication”和“ REACTOR”)。列标题应为“ Waste_ads(吨)”,依此类推。行索引(-5,-4,-3,-2 ... 5)
data = {'Fabrication': {'Waste_ads (tons)': {-5.0: 0.0,-4.0: 0.0,-3.0: 0.0,-2.0: 0.0,-1.0: 0.0,0.0: 0.0,1.0: 0.0,2.0: 0.0,3.0: 0.0,4.0: 0.0,5.0: 0.0},'Uox_spent (tons)': {-5.0: 0.0,'Depu (tons)': {-5.0: 0.0,-2.0: 42752.30175388285,-1.0: 8083.238467166332,0.0: 8083.238467166332,1.0: 8083.238467166332,2.0: 8083.238467166332,3.0: 8083.238467166332,4.0: 8083.238467166332,5.0: 8083.238467166332},'U (tons)': {-5.0: 0.0,'Uox2_spent (tons)': {-5.0: 0.0,'Uox2 (tons)': {-5.0: 0.0,'Pu (tons)': {-5.0: 0.0,'Waste_mox (tons)': {-5.0: 0.0,'Waste_uox2 (tons)': {-5.0: 0.0,'Waste_uox1 (tons)': {-5.0: 0.0,'Adsfuel_spent (tons)': {-5.0: 0.0,'Mox (tons)': {-5.0: 0.0,'Nat (tons)': {-5.0: 0.0,'Uox (tons)': {-5.0: 0.0,-2.0: 4649.423,-1.0: 5528.496016,0.0: 6407.569031999999,1.0: 2637.219048,2.0: 2637.219048,3.0: 2637.219048,4.0: 2637.219048,5.0: 2637.219048},'Mox_spent (tons)': {-5.0: 0.0,'Ma (tons)': {-5.0: 0.0,'Adsfuel (tons)': {-5.0: 0.0,5.0: 0.0}},'REACTOR': {'Waste_ads (tons)': {-5.0: 0.0,1.0: 879.0730159999998,2.0: 1758.1460319999996,4.0: 3516.292064,5.0: 4395.3650800000005},1.0: 3770.349984,2.0: 3770.3499840000004,3.0: 3770.3499840000004,4.0: 3770.3499840000004,5.0: 3770.3499840000004},5.0: 0.0}}}
在今年更新Python之前,我曾使用过Pandas的Panel,现在无法使用了。我的尝试变得太复杂了,我需要一个更好的方法。
感谢您的帮助
SS:
解决方法
这适用于您在上面提供的data
; data
是具有两个键(“ Fabrication”和“ REACTOR”)的字典。每个值都可以转换为数据帧。如果最终输出中需要“制造”和“ REACTOR”,则可以修改以下示例。
import pandas as pd
dfs = (pd.DataFrame(d) for d in data.values())
df = pd.concat(dfs,axis=1)
print(df.shape)
(11,34)
更新
如果需要保留工作表名称,可以这样做。很抱歉提出两个选择,但我不确定预期的结果。
df = list()
for key,value in data.items():
t = pd.DataFrame(value)
t['sheetname'] = key
df.append(t)
df = pd.concat(df)
print(df.iloc[0:5,0:4])
Waste_ads (tons) Uox_spent (tons) Depu (tons) U (tons)
-5.0 0.0 0.0 0.000000 0.0
-4.0 0.0 0.0 0.000000 0.0
-3.0 0.0 0.0 0.000000 0.0
-2.0 0.0 0.0 42752.301754 0.0
-1.0 0.0 0.0 8083.238467 0.0