Professional Documents
Culture Documents
INTRODUCTION
A pivot table is a statistical table that summarizes
a substantial table like a big dataset.
It is part of data processing.
This summary in pivot tables may include mean,
median, sum, or other statistical terms.
Pivot tables are originally associated with MS
Excel but we can create a pivot table in Pandas
using Python using the Pandas Dataframe
pivot_table() method.
WORKING OF PIVOT TABLES
• Import Libraries: First import Pandas library
which provides functionality for data
manipulation and analysis.
• Create DataFrame: Create a DataFrame
containing your data.
• Create a Pivot Table: Use the pivot_table()
method of the DataFrame to create a pivot table.
• View the Pivot Table: View the resulting pivot
table by printing it or displaying it.
SYNTAX
pivot = df.pivot_table(index=[’ COLUMN_NAME],
values=[’COLUMN_NAME],
aggfunc=’ NAME OF FUNCTION)
• index specifies which column to use as the
index in the pivot table.
• columns specifies which column to use to
create new columns in the pivot table.
• values specifies which column(s) to aggregate.
• aggfunc specifies the aggregation function to
use when multiple values need to be
combined.
import pandas as pd
data = {'Student': ['Akash', 'Aman', 'bharti', 'Umang',
'Satish'],
'Computers': [35, 35, 40, 40, 20],
'Maths': [50, 20, 30, 40, 10],
'Residence': ['HP', 'chd','Haryana', 'Punjab',
'HP'],
'Gender': ['M', 'M', 'F','F', 'M']
}
df = pd.DataFrame(data)
df
pivot = df.pivot_table(index=['Student'],
values=['Maths', 'Computers'],
aggfunc='sum')
print(pivot)
pivot = df.pivot_table(index=['Gender'],
values=['Maths'],
aggfunc='sum')
print(pivot)
ADVANTAGES
• Ease of Use: Python's Pandas library provides an intuitive interface
for creating pivot tables. With just a few lines of code, you can
generate complex pivot tables to summarize and analyze your data.