0

I have a dataframe 'Top15' whose index is country :

Top15.index
Index(['China', 'United States', 'Japan', 'United Kingdom',
   'Russian Federation', 'Canada', 'Germany', 'India', 'France',
   'South Korea', 'Italy', 'Spain', 'Iran', 'Australia', 'Brazil'],
  dtype='object', name='Country')

I have a dictionary which has continent for each country.

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'}

I want to group the countries by continent. I created a column 'Continent'

Top15['Continent']=Top15.index.map(ContinentDict)

After that i tried to group by continent

Top15.groupby('Continent')

I received following output:

<pandas.core.groupby.generic.DataFrameGroupBy object at 0x0000023CBF691AC8>

When i checked the dataframe, it was not grouped. Is it because country is index and not in column? What should i do?

Ch3steR
  • 19,076
  • 4
  • 25
  • 52
nona
  • 17
  • 3
  • 1
    Unfortunately you can't do that. To get each each group `for k,g in Top15.groupby('Continent'): print(k);print(g)` – Ch3steR Jun 05 '20 at 19:23
  • 2
    The `GroupBy` object is just really a collection, it contains a mapping from the original DataFrame to your groups. You need to do something to that to get back a Series or DataFrame. – ALollz Jun 05 '20 at 19:23
  • You might find this useful: https://stackoverflow.com/questions/61810108/why-does-groupby-operations-behave-differently, also https://stackoverflow.com/questions/62092600/how-does-apply-work-on-a-pandas-dataframe/62092807#62092807 – ALollz Jun 05 '20 at 19:26

0 Answers0