I'm trying to create an excel file with the content of the list games
below. I want to create DATA
sheet with
a table containing the information of games
list. Then I'd like to have PIVOT
sheet containing a pivot table
with season
field as first column and filled down (not in merge cell), to finally add a formula in same 'PIVOT' sheet.
My current code is:
import pandas as pd
import xlsxwriter
games =[
['Summer','China','CHN','Gold'],['Summer','France','FRA','Gold'],['Summer','Great Britain','GBR','Gold'],
['Summer','Soviet Union','URS','Gold'],['Summer','United States','USA','Gold'],['Summer','Germany','GER','Silver'],
['Summer','Hungary','HUN','Silver'],['Summer','Italy','ITA','Silver'],['Summer','Japan','JPN','Silver'],
['Summer','Australia','AUS','Bronze'],['Summer','East Germany','GDR','Bronze'],['Summer','Finland','FIN','Bronze'],
['Summer','Russia','RUS','Bronze'],['Summer','Sweden','SWE','Bronze'],['Winter','Austria','AUT','Gold'],
['Winter','Canada','CAN','Gold'],['Winter','Germany','GER','Gold'],['Winter','Norway','NOR','Gold'],
['Winter','Soviet Union','URS','Gold'],['Winter','United States','USA','Gold'],['Winter','Netherlands','NED','Silver'],
['Winter','Sweden','SWE','Silver'],['Winter','Switzerland','SUI','Silver'],['Winter','East Germany','GDR','Bronze'],
['Winter','Finland','FIN','Bronze'],['Winter','France','FRA','Bronze'],['Winter','Italy','ITA','Bronze'],
['Winter','Russia','RUS','Bronze'],['Winter','South Korea','KOR','Bronze'],['Combined','China','CHN','Gold'],
['Combined','Germany','GER','Gold'],['Combined','Great Britain','GBR','Gold'],['Combined','Soviet Union','URS','Gold'],
['Combined','United States','USA','Gold'],['Combined','East Germany','GDR','Silver'],['Combined','France','FRA','Silver'],
['Combined','Italy','ITA','Silver'],['Combined','Norway','NOR','Silver'],['Combined','Russia','RUS','Silver'],
['Combined','Sweden','SWE','Silver'],['Combined','Australia','AUS','Bronze'],['Combined','Canada','CAN','Bronze'],
['Combined','Hungary','HUN','Bronze'],['Combined','Japan','JPN','Bronze']
]
workbook = pd.ExcelWriter('file6.xlsx', engine='xlsxwriter')
df = pd.DataFrame(games, columns=['Season','Country','Abbr','Type'])
df.to_excel(workbook, sheet_name='DATA')
p_table = pd.pivot_table(df, index = ['Season','Abbr','Type'])
p_table.to_excel(workbook, sheet_name='PIVOT')
pivot_sheet = workbook.worksheet('PIVOT')
pivot_sheet.write_array_formula('F3:F3', '{=SUM(D1:E1*D2:E2)}')
workbook.close()
I'm able to create book with table in DATA
sheet, with pivot table in PIVOT
sheet, but I'm stuck in how to add the formula
in the PIVOT
sheet and how to get first column without merged cells.
This is the error when try to assign PIVOT
sheet to a variable to be able to write_array_formula
in next step. Thanks in advance
>>> pivot_sheet = workbook.sheets('PIVOT')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'dict' object is not callable
UPDATE I've been able to add the pivot table without merging cells with command below. Still need help in how to add the array formula in PIVOT sheet.
p_table.reset_index().to_excel(workbook, sheet_name='PIVOT', index=False)