OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

python pandas loop through dataframe replicate many tables in excel

  • Thread starter Thread starter John
  • Start date Start date
J

John

Guest
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!!

Code:
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):

Code:
  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
<p>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!!</p>
<pre><code>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')
</code></pre>
<p>Proposed outcome in excel (one row is skipped between id groupings):</p>
<pre><code> 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
</code></pre>
 

Latest posts

Online statistics

Members online
0
Guests online
3
Total visitors
3
Top