Python Forum

Full Version: How to pivot a dat
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
All,
I would like to pivot a data frame similar to the following SQL operation. Any help/guidance is appreciated:

# SQL Code - https://www.sqlshack.com/static-and-dynamic-sql-pivot-and-unpivot-relational-operator-overview/
SELECT * FROM 
(
SELECT YEAR(SOH.OrderDate) as SalesYear,
        SOH.SubTotal as TotalSales
 FROM sales.SalesOrderHeader SOH
     JOIN sales.SalesOrderDetail SOD ON SOH.SalesOrderId = SOD.SalesOrderId
) AS Sales
PIVOT (SUM(TotalSales)
FOR SalesYear IN ([2011],[2012],[2013],[2014]))
as PVT