Related Posts
Additional Posts in Data Analytics & Data Science
New to Fishbowl?
Download the Fishbowl app to
unlock all discussions on Fishbowl.
unlock all discussions on Fishbowl.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
Download the Fishbowl app to unlock all discussions on Fishbowl.
Copy and paste embed code on your site

Scan your QR code to download
Fishbowl app on your mobile

import pandas as pd
# Sample data
data = {
'Date': ['2023-01-01', '2023-01-02', '2023-01-03', '2023-01-01', '2023-01-02'],
'Region': ['East', 'West', 'East', 'North', 'South'],
'Sales': [100, 150, 200, 130, 170]
}
df = pd.DataFrame(data)
# Create a pivot table
pivot_table = df.pivot_table(index='Date', columns='Region', values='Sales', aggfunc='sum')
# Write to an Excel file
excel_file = 'pivot_table.xlsx'
with pd.ExcelWriter(excel_file, engine='xlsxwriter') as writer:
pivot_table.to_excel(writer, sheet_name='PivotTable')
# Get the xlsxwriter workbook and worksheet objects.
workbook = writer.book
worksheet = writer.sheets['PivotTable']
# Add some formatting
format1 = workbook.add_format({'num_format': '#,##0.00'})
# Set the column width and format
worksheet.set_column('A:A', 15, format1)
worksheet.set_column('B:D', 10, format1)
See if this is helpful: Create Pivot Tables in Excel Using Python https://medium.com/@alexaae9/screate-pivot-tables-in-excel-using-python-0d9d131ac2e0
Give chatgpt a try for things like that! Had a lot of success there with excel help. Even just asking basic things, what is the best way to do [insert thing]. Whether or not it is the best way, not always sure, but it is always a way to do it! Definitely give it a try next time you are stuck on something.
I second the chat gpt recommendation. I will sometimes describe my df and what I want to accomplish and then work back and forth with it until I have what I want. Works surprisingly well tbh
f
Literally use .pivot_table() in pandas, lol I’m sure you can find some examples if you google it
I think ChatGPT can help you