Search code examples
pythonpandasexport-to-excel

How to replace Panel in Pandas and export it to excel


I have this dataset and I wanna export it as excel file (as the picture) in two sheets ('Fabrication' and 'REACTOR') The column head should be 'Waste_ads (tons)' and so on. The row index (-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, -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}, 'Depu (tons)': {-5.0: 0.0, -4.0: 0.0, -3.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, -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}, 'Uox2_spent (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}, 'Uox2 (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}, 'Pu (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}, 'Waste_mox (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}, 'Waste_uox2 (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}, 'Waste_uox1 (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}, 'Adsfuel_spent (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}, 'Mox (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}, 'Nat (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 (tons)': {-5.0: 0.0, -4.0: 0.0, -3.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, -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}, 'Ma (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}, 'Adsfuel (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}}, 'REACTOR': {'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, -4.0: 0.0, -3.0: 0.0, -2.0: 0.0, -1.0: 0.0, 0.0: 0.0, 1.0: 879.0730159999998, 2.0: 1758.1460319999996, 3.0: 2637.219048, 4.0: 3516.292064, 5.0: 4395.3650800000005}, 'Depu (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}, 'U (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}, 'Uox2_spent (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}, 'Uox2 (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}, 'Pu (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}, 'Waste_mox (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}, 'Waste_uox2 (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}, 'Waste_uox1 (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}, 'Adsfuel_spent (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}, 'Mox (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}, 'Nat (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 (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: 3770.349984, 2.0: 3770.3499840000004, 3.0: 3770.3499840000004, 4.0: 3770.3499840000004, 5.0: 3770.3499840000004}, 'Mox_spent (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}, 'Ma (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}, 'Adsfuel (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}}}

I used Panel from Pandas before I updated Python this year and I cannot use it anymore. My attempt is getting too complicated and I need a better approach.

Thank you for your help

SS:

enter image description here


Solution

  • This works for the data you provided above; data is a dictionary with two keys ('Fabrication' and 'REACTOR'). Each of the values can be converted to a data frame. You can modify the example below if you need 'Fabrication' and 'REACTOR' in the final output.

    import pandas as pd
    dfs = (pd.DataFrame(d) for d in data.values())
    
    df = pd.concat(dfs, axis=1)
    print(df.shape)
    
    (11, 34)
    

    UPDATE

    In case you need to keep sheet names, you can to this. Sorry for presenting two choices, but I'm not certain of the expected results.

    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