pythonexcelpandasreport

python pandas loop through dataframe replicate many tables in excel


I want to convert a large dataframe to a series of report tables that replicates the template for each unique id within the dataframe seperated/skipped excel row. I would like to do this with a series of loops. I think I can accomplish through mapping each item in the df to an excel file... but it would take several thousand lines based on the size of the dataframe - any help would be much appreciated!!

import pandas as pd

data = {'id' = [1,2,3]
  , 'make' = ['ford','chevrolet','dodge']
  , 'model' = ['mustang','comaro','challenger']
  , 'year' = ['1969','1970','1971']
  , 'color' = ['blue', 'red', 'green']
  , 'miles' = ['15000','20000','35000']
  , 'seats' = ['leather', 'cloth' , 'leather']
  }
df = pd.DataFrame(data)

df.to_excel(r'/desktop/reports/output1.xlsx')

Proposed outcome in excel (one row is skipped between id groupings):

  A       B             C       D        E        F 
1 make    ford          year    1969     miles    15000
2 model   mustang       color   blue     seats    leather
3 
4 make    chevrolet     year    1970     miles    20000
5 model   comaro        color   red      seats    cloth
6
7 make    dodge         year    1971     miles    35000
8 model   challenger    color   green    seats    leather

Solution

  • Code

    i think don't need loop. reshape your data frame

    # melt & sort
    tmp = df.melt('id', var_name='a', value_name='b').sort_values('id', kind='stable')
    
    # make id's cumcount to variable s
    s = tmp.groupby('id').cumcount()
    
    # assign 'row' and 'col' column based on variable s
    tmp['row'], tmp['col'] = s % 2, s // 2
    
    # pivot & sort
    tmp = (tmp.pivot(index=['id', 'row'], columns='col')
              .swaplevel(0, 1, axis=1).sort_index(axis=1).droplevel(0, axis=1)
    )
    

    tmp:

    enter image description here

    # create variable idx(MultiIndex) for making blank rows
    idx = pd.MultiIndex.from_product([df['id'].unique(), [0, 1, 2]])
    
    # reindex with idx & save to Excel file without index and header
    tmp.reindex(idx).to_excel('result.xlsx', index=False, header=False)
    

    result.xlsx:

    enter image description here