1

I have a dataframe in the form {'country','year','value'}. I want to move it in the form of:

country.........1961....1962....1963
countryname.....value...value...value.

Therefore I have used df1=df.pivot(index='country',column='year',value='value')

the output comes in the form year..........1961....1962....1963
country
countryname...value...value...value

Now I would like to replace the long official names used for some countries and insert a shorter name (i.e. Islamic Republic of Iran --> Iran). I have a second dataframe that contains these couples and am trying to get the index labels with

for i in df1.loc[:,['country']]

but i get back the Key error: 'country'. I tried using 'year' instead of 'country' but with the same result.

How do I reference the index after pivoting?

Your help would be greatly appreciated.

  • Does this help? [Accessing a Pandas index like a regular column](https://stackoverflow.com/q/52139506/13138364) – tdy Apr 17 '21 at 08:27
  • please check this url https://stackoverflow.com/questions/38951345/how-to-get-rid-of-multilevel-index-after-using-pivot-table-pandas – Deepak Apr 17 '21 at 08:28
  • 1
    Dear try and Deepak, Thanks, your answers helped me understanding what I was doing wrong. – Marco Spurio Cassio Apr 17 '21 at 15:26
  • will you provide an sample of the dataframe data and I will show you have to get the index of the pivot table. the pivot table uses the dataframe index. I suppose your using a datetimeindex field for the index. – Golden Lion Apr 17 '21 at 21:14

0 Answers0