Pandas How To Uncategorized How to pivot a dataframe in Pandas?

How to pivot a dataframe in Pandas?

In Pandas, you can use the pivot function to pivot a dataframe. The pivot function reshapes a dataframe by converting its rows into columns or columns into rows based on a specified column. Here’s an example:

Suppose you have the following dataframe:

import pandas as pd

df = pd.DataFrame({
'name': ['Alice', 'Bob', 'Charlie'],
'gender': ['F', 'M', 'M'],
'math': [90, 85, 70],
'science': [80, 75, 85]
})

print(df)

Output:

name gender math science
0 Alice F 90 80
1 Bob M 85 75
2 Charlie M 70 85

You can pivot this dataframe to have the columns math and science as rows, and the column name as columns, by using the pivot function as follows:

pivoted_df = df.pivot(index='gender', columns='name')

print(pivoted_df)

Output:

math science
name Alice Bob Charlie Alice Bob Charlie
gender
F 90 NaN NaN 80.0 NaN NaN
M NaN 85 70.0 NaN 75.0 85.0

In the above example, the index parameter specifies the column to be used as the new index, and the columns parameter specifies the column to be used as the new columns. The resulting dataframe has two levels of column labels, with the outer level being the original column name (math and science), and the inner level being the original row values (Alice, Bob, and Charlie). The values in the resulting dataframe are the original values of the math and science columns for each person.

Note that pivot assumes that the combination of values in the specified index and column columns is unique, and will raise an error if it is not. If you need to handle duplicates, you can use the pivot_table function instead.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post