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.
df.pivot_table()Output:
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.
pd.crosstab()pd.crosstab() is specifically designed for computing frequency tables (counts or percentages of occurrences between two categorical variables):
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?Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Multiple Aggregations & NamedAgg | Concatenating DataFrames (pd.concat) |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.
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.