Python Answers

How to pivot a dataframe in Python Pandas?

Spread the love

To pivot a dataframe in Python Pandas, we call the pivot_table method.

For instance, we write

pd.pivot_table(df, values = 'Value', index=['Country','Year'], columns = 'Indicator').reset_index()

to call pivot_table with the df data frame.

We set the values argument to the olumn with the values.

index has the index columns.

columns has other columns.

And we call reset_index to reassign the indices for each row.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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