1

I want to rename the index from 'Country' to 'Continent'. What should I change in this code to rename the index?

    answer=answer_1c()
    answer['Population_Estimate'] = answer['Energy Supply'] / answer['Energy Supply per Capita']
    answer['Population_Estimate'] = np.float64(answer['Population_Estimate'])
    ContinentDict  = {'China':'Asia', 
                      'United States':'North America', 
                      'Japan':'Asia', 
                      'United Kingdom':'Europe', 
                      'Russian Federation':'Europe', 
                      'Canada':'North America', 
                      'Germany':'Europe', 
                      'India':'Asia',
                      'France':'Europe', 
                      'South Korea':'Asia', 
                      'Italy':'Europe', 
                      'Spain':'Europe', 
                      'Iran':'Asia',
                      'Australia':'Australia', 
                      'Brazil':'South America'}
    answer.rename(index=ContinentDict,inplace=True)
    answer.reset_index(inplace = True)
    functions = ['size', 'sum', 'mean', 'std']
    result = answer[['Country', 'Population_Estimate']].groupby('Country').agg(functions)
    return(result)

Output: enter image description here

  • 1
    Does this answer your question? [Rename Pandas DataFrame Index](https://stackoverflow.com/questions/19851005/rename-pandas-dataframe-index) – Umair Mubeen Jan 24 '21 at 19:27

1 Answers1

0

if you want to change in place

result.index.rename('Continent', inplace=True)

of assign new dataframe to existing:

result = result.index.rename('Continent')
godot
  • 3,422
  • 6
  • 25
  • 42