A2oz

Which command is used to pivot a table in Pandas?

Published in Data Analysis 2 mins read

The pivot_table() function is used to pivot a table in Pandas.

Understanding Pivot Tables

A pivot table is a powerful tool for summarizing and analyzing data. It allows you to rearrange your data based on multiple criteria, creating a new table that provides insights into relationships and patterns within your dataset.

How to Use pivot_table()

Here's a basic example of how to use pivot_table():

import pandas as pd

data = {'Name': ['Alice', 'Bob', 'Charlie', 'Alice', 'Bob'],
        'Age': [25, 30, 28, 25, 30],
        'City': ['New York', 'London', 'Paris', 'Los Angeles', 'Tokyo']}

df = pd.DataFrame(data)

# Pivot the table with 'Name' as rows, 'City' as columns, and 'Age' as values
pivot_table = df.pivot_table(values='Age', index='Name', columns='City')

print(pivot_table)

This code will create a pivot table with:

  • Rows: Names of individuals
  • Columns: Cities they live in
  • Values: Their ages

Key Arguments of pivot_table()

The pivot_table() function offers several arguments to customize your pivot table:

  • values: The column containing the values you want to aggregate.
  • index: The column(s) to use as row labels.
  • columns: The column(s) to use as column labels.
  • aggfunc: The function used to aggregate the values. Default is mean. Other options include sum, min, max, etc.

Practical Insights

  • You can use multiple columns for index and columns to create a more complex pivot table.
  • The aggfunc argument allows you to perform different types of aggregation based on your needs.
  • Pivot tables are particularly useful for analyzing data with multiple dimensions, such as sales data by region, product, and time.

Related Articles