How to pivot a dataframe in Pandas? [duplicate] How to pivot a dataframe in Pandas? [duplicate] python python

How to pivot a dataframe in Pandas? [duplicate]


You can use pivot_table:

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

this outputs:

 Indicator  Country     Year    1   2   3   4   5 0          Angola      2005    6   13  10  11  5 1          Angola      2006    3   2   7   3   6


This is a guess: it's not a ".csv" file, but a Pandas DataFrame imported from a '.csv'.

To pivot this table you want three arguments in your Pandas "pivot". e.g., if df is your dataframe:

table = df.pivot(index='Country',columns='Year',values='Value')  print (table)

This should should give the desired output.