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.