Search code examples
pythonpandasdataframeexport-to-excel

Transform excel table looping through columns


I'm working on a Excel table transformation into another file for database upload. The tables usually looks like this: enter image description here

The result should be a long list looking like this:

enter image description here

And this is the code I was trying to use...any thoughts?

import pandas as pd
from pandas import DataFrame
import numpy as np


df_excel = pd.read_excel('Excel_Forecast.xlsx', engine='openpyxl')

df_details = df_excel['Details']

df_base = []

for column in df_excel.columns[2:]:
    df_base['Details'].append(df_excel['Details'])
    df_base = DataFrame(df_base.append(df_excel[(column)]),columns=['Amount'])

df_base.to_excel('Temp.xlsx', index=False)

Solution

  • Use df.melt:

    df.melt(['Group', 'Item'])