
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Create a Pivot Table with multiple columns – Python Pandas
We can create a Pivot Table with multiple columns. To create a Pivot Table, use the pandas.pivot_table to create a spreadsheet-style pivot table as a DataFrame.
At first, import the required library −
import pandas as pd
Create a DataFrame with Team records −
dataFrame = pd.DataFrame({'Team ID': {0: 5, 1: 9, 2: 6, 3: 11, 4: 2, 5: 7 },'Team Name': {0: 'India', 1: 'Australia', 2: 'Bangladesh', 3: 'South Africa', 4: 'Sri Lanka', 5: 'England'},'Team Points': {0: 95, 1: 93, 2: 42, 3: 60, 4: 80, 5: 55},'Team Rank': {0: 'One', 1: 'Two', 2: 'Six', 3: 'Four', 4: 'Three', 5: 'Five'}})
Create a Pivot Table with multiple columns. We have set more than more than two columns −
pd.pivot_table(dataFrame, index = ["Team ID", "Team Name", "Team Rank"])
Example
Following is the code −
import pandas as pd # create DataFrame with Team records dataFrame = pd.DataFrame({'Team ID': {0: 5, 1: 9, 2: 6, 3: 11, 4: 2, 5: 7 },'Team Name': {0: 'India', 1: 'Australia', 2: 'Bangladesh', 3: 'South Africa', 4: 'Sri Lanka', 5: 'England'},'Team Points': {0: 95, 1: 93, 2: 42, 3: 60, 4: 80, 5: 55},'Team Rank': {0: 'One', 1: 'Two', 2: 'Six', 3: 'Four', 4: 'Three', 5: 'Five'}}) print("\n... Pivot ...") # multiple columns print(pd.pivot_table(dataFrame, index = ["Team ID", "Team Name", "Team Rank"]))
Output
This will produce the following output −
... Pivot ... Team Points Team ID Team Name Team Rank 2 Sri Lanka Three 80 5 India One 95 6 Bangladesh Six 42 7 England Five 55 9 Australia Two 93 11 South Africa Four 60
- Related Articles
- Create a Pivot Table as a DataFrame – Python Pandas
- How to create conditions in a MySQL table with multiple columns?
- Python - Select multiple columns from a Pandas dataframe
- How we have multiple stored GENERATED COLUMNS in MySQL table with CREATE TABLE statement?
- Python Pandas - Plot multiple data columns in a DataFrame?
- How can we have multiple virtuals GENERATED COLUMNS in MySQL table with CREATE TABLE statement?
- Python - Create a Time Series Plot with multiple columns using Line Plot
- Python Pandas – Find unique values from multiple columns
- How to add multiple fields into a pivot table in Excel?
- Python Pandas - Create a DataFrame with the levels of the MultiIndex as columns
- Select multiple columns in a Pandas DataFrame
- How to create pivot table with sum for data stored in R data frame?
- Python Pandas - Create a datetime with DateTimeIndex
- How to sort multiple columns of a Pandas DataFrame?
- How to create pivot table with sum for data stored in data.table object in R?

Advertisements