MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Pivot Tables & Cross-Tabulations (pivot_table, crosstab)0%
Study MaterialsCheatsheet
PandasProgress 0%
1.1What is Pandas & Core Architecture1.2Pandas Installation & Environment Setup1.3Creating Series & DataFrames from Scratch
6.1GroupBy Mechanics: Split-Apply-Combine6.2Multiple Aggregations & NamedAgg6.3Pivot Tables & Cross-Tabulations (pivot_table, crosstab)
HomeTutorialsPandasPivot Tables & Cross-Tabulations (pivot_table, crosstab)
Concatenating DataFrames (pd.concat)

Pivot Tables & Cross-Tabulations (pivot_table, crosstab)

Intermediate6 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study MaterialsCheatsheet

1. Pivot Tables in Pandas

If you know PivotTables in Microsoft Excel, pd.pivot_table() will feel instantly familiar. A Pivot Table aggregates data and reshapes it into a two-dimensional grid with designated row headers, column headers, and calculated cell values.


2. Anatomy of df.pivot_table()

1import pandas as pd
2 
3df = pd.DataFrame({
4 'Year': [2024, 2024, 2025, 2025, 2024, 2025],
5 'Region': ['North', 'South', 'North', 'South', 'North', 'South'],
6 'Product': ['Laptop', 'Laptop', 'Laptop', 'Tablet', 'Tablet', 'Laptop'],
7 'Sales': [50000, 60000, 55000, 30000, 35000, 65000]
8})
9 
10# Create Pivot Table: Rows = Region, Columns = Product, Values = Total Sales
11pivot = df.pivot_table(
12 index='Region',
13 columns='Product',
14 values='Sales',
15 aggfunc='sum',
16 fill_value=0, # Replace NaN with 0 for product-region combinations with no sales
17 margins=True, # Adds 'All' row and column subtotals (Grand Totals!)
18 margins_name='Total'
19)
20print(pivot)

Output:

1Product Laptop Tablet Total
2Region
3North 105000 35000 140000
4South 125000 30000 155000
5Total 230000 65000 295000

3. What Does margins=True Do?

In Excel, PivotTables automatically append Grand Total rows and columns. In Pandas, setting margins=True generates these subtotal rows and columns automatically.


4. Cross-Tabulations with pd.crosstab()

pd.crosstab() is specifically designed for computing frequency tables (counts or percentages of occurrences between two categorical variables):

1# Frequency count table of Region vs Product
2freq_table = pd.crosstab(df['Region'], df['Product'])
3print(freq_table)
4 
5# Normalized percentage table (Proportions that sum to 100%)
6pct_table = pd.crosstab(df['Region'], df['Product'], normalize='index') * 100
7print(pct_table)

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
What parameter in df.pivot_table() specifies the mathematical aggregation to apply (defaulting to 'mean')?
Question 2
What does margins=True add to a Pandas pivot table?
Question 3
How do you replace empty NaN cells in a pivot table with 0?
Question 4
What is the primary use case for pd.crosstab()?
Question 5
In pd.crosstab(df['A'], df['B'], normalize='index'), what does normalize='index' do?

Next Lesson

Concatenating DataFrames (pd.concat)

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Multiple Aggregations & NamedAggConcatenating DataFrames (pd.concat)

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

12-Month Data Analysis Mastery Program

Master Full-Stack Data Analytics (Excel, SQL, Python, Power BI & Tableau)

Combine 15 complete courses, 150 chapters, 541 topics, live SQL/Power BI/Tableau dashboards, and Scikit-Learn capstones at MSK Institute Shikohabad or Live Online.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext