Pivot Tables & Cross-Tabulations (pivot_table, crosstab)
Pivot Tables & Cross-Tabulations (pivot_table, crosstab)
Reading Time: 8 Mins | Level: Beginner to Advanced
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()
Output:
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):
Multiple Choice Questions
df.pivot_table() specifies the mathematical aggregation to apply (defaulting to 'mean')?margins=True add to a Pandas pivot table?pd.crosstab()?pd.crosstab(df['A'], df['B'], normalize='index'), what does normalize='index' do?Concatenating DataFrames (pd.concat)
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Multiple Aggregations & NamedAgg | Concatenating DataFrames (pd.concat) |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.