Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to implement a PivotTable in Python

Shulou Source: shulou.com Published: 2022-06-02 07:16:24 09月22日 Update

This article to share with you is about how to implement a pivot table in Python, Xiaobian feel quite practical, so share to everyone to learn, I hope you can read this article after some gains, not much to say, follow Xiaobian to see it.

1. Preview data sources

#Preview Data Source df

2. Calculate the average number of sales per year

#Calculate the average number of sales per year pd.pivot_table(df, index = "Year", values = "Number of Sales" )

3. Calculate total sales for each year

#Calculate total sales per year pd.pivot_table(df, index = "year", values = "sales" ,aggfunc = "sum")

4. Calculate the total sales volume of various commodities in each province

#Calculate the total sales volume of various commodities in each province pd.pivot_table(df, index = "province", columns = "commodity", values = "sales volume" ,aggfunc = "sum")

5. Calculate the total sales quantity of various commodities in each province, and force the error value to 0

#Calculate the total sales quantity of various commodities in each province and force the error value to be 0pd.pivot_table(df, index = "province", columns = "commodity", values = "sales quantity" ,aggfunc = "sum").fillna("0")

6. Calculate the total sales quantity of various commodities in each province, and force the error value to be displayed as 0. Add total rows and columns

#Calculate the total sales quantity of various commodities in each province and force the error value to 0. Add the total row and column pd.pivot_table(df, index = "province", columns = "commodity", values = "sales quantity" ,aggfunc = "sum",margins = True).fillna("0")

7. #Calculate the total sales quantity of various commodities in each province, force the error value to display 0. Add total rows and columns, and rename total rows

#Calculate the total sales quantity of various commodities in each province and force the error value to 0. Add the total row and column and rename the total row pd.pivot_table(df, index = "province", columns = "commodity", values = "sales quantity" ,aggfunc = "sum",margins = True,margins_name = "total").fillna("0")

The above is how to implement a pivot table in Python, Xiaobian believes that some knowledge points may be seen or used in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Tags: Quantity sales goods province year error compulsion data data source more knowledge article practical that is work meeting article look knowledge point industry details Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB macOS NVidia OPPO Reno MySQL