How can I pivot a dataframe?

Discussion RoomCategory: Data ScienceHow can I pivot a dataframe?
Ashly asked 9 months ago

Pivoting a DataFrame is a common operation in data manipulation, especially when dealing with tabular data. In pandas, a popular Python library for data analysis, you can pivot a DataFrame using the `pivot()` or `pivot_table()` functions. Let’s go through both methods:
1. Using `pivot()` function:
The `pivot()` function reshapes the DataFrame based on unique values in a specified column, creating a new DataFrame with columns from the values in that column. It requires that the combination of the index and columns be unique.
“`python
import pandas as pd
# Sample DataFrame
data = {
‘Date’: [‘2023-01-01’, ‘2023-01-01’, ‘2023-01-02’, ‘2023-01-02’],
‘Category’: [‘A’, ‘B’, ‘A’, ‘B’],
‘Value’: [10, 20, 15, 25]
}
df = pd.DataFrame(data)
# Pivot the DataFrame
pivot_df = df.pivot(index=’Date’, columns=’Category’, values=’Value’)
print(pivot_df)
“`
2. Using `pivot_table()` function:
The `pivot_table()` function provides more flexibility and can handle cases where there might be duplicate index/column combinations. It also supports aggregating data when multiple values fall under a single combination.
“`python
import pandas as pd
# Sample DataFrame
data = {
‘Date’: [‘2023-01-01’, ‘2023-01-01’, ‘2023-01-02’, ‘2023-01-02’],
‘Category’: [‘A’, ‘B’, ‘A’, ‘B’],
‘Value’: [10, 20, 15, 25]
}
df = pd.DataFrame(data)
# Pivot the DataFrame using pivot_table
pivot_df = df.pivot_table(index=’Date’, columns=’Category’, values=’Value’, aggfunc=’sum’)
print(pivot_df)
“`
In both examples, the resulting DataFrame will have unique dates as the index and categories as columns, with the corresponding values filled in the cells.
Keep in mind that pivoting can lead to NaN (Not a Number) values in the resulting DataFrame if some index/column combinations are missing in the original DataFrame. You might need to handle NaN values based on your use case.
Additionally, if you’re working with a more complex scenario, such as multiple columns for values or more intricate aggregations, you might need to adjust the parameters accordingly in the `pivot_table()` function.

Scroll to Top