Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Panda pivot_table questions
#1
I need help on Panda pivot_table. The following are the code and the output. I will need help on:
1. Why Product and the Term are not in the output?
2. The columns in Excel are automatically in alphabetic order. How can I have the order in the order I specified ['Current_Bal','Amount1','Amount2']?
3. How to make it like Excel pivot table that has subtotal by Product and a grand total?
Thank you.

sql = """
select Product
	, Term
	, Current_Bal
	, Amount1
	, Amount2
from working.Temp_Ares_Data_Since_Aquisition
where As_of_date = '2022-12-31'"""

df = pd.read_sql(sql,cnxn)

pivot = pd.pivot_table(df, values=['Current_Bal','Amount1','Amount2'], index=['Product','Term'],aggfunc='sum')

writer = pd.ExcelWriter(fullpath)
pivot.to_excel(writer, sheet_name='Pivot', index=False)
writer.save()   
Output:
Amount1 Amount2 Current_Bal 100000 150000 50000
Reply
#2
I figured out my question #1. Remove index=False in pivot.to_excel(writer, sheet_name='Pivot', index=False).
For #2, the order is in the order I specified when I created another Python scripts. Not sure why.
I will still need help on #3. The Grand Total is incorrect for calculated columns. I also need help on how to have subtotal.
Thanks.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Remove values for weekend in a panda series JaneTan 0 671 Dec-12-2022, 01:50 AM
Last Post: JaneTan
  panda table data kucingkembar 0 1,117 Mar-01-2022, 10:38 PM
Last Post: kucingkembar
  can we write command output to new csv file using Panda package? PythonBeginner_2020 3 2,370 Mar-13-2020, 12:38 PM
Last Post: ndc85430
  Header above a list in Panda sweet_swiss 3 2,958 Aug-12-2018, 11:03 PM
Last Post: scidam
  Discord bot that asks questions and based on response answers or asks more questions absinthium 1 38,359 Nov-25-2017, 06:21 AM
Last Post: heiner55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020