简体   繁体   中英

Using 'groupby' without aggregation and sorting within groups

I have countries_of_the_world.csv . Basically, it's the table with the following bits of information:

Country    Region             GDP
Austria    Western Europe     100
Chad       Africa             30

I need to sort GDP values in descending order by region with countries inside these regions. It should look like:

Region     Country        GDP
Africa     Egypt          42
           Chad           30
           Kongo          28
Oceania    Australia      120
           New Zealand    100
           Indonesia      50

I tried 'groupby' but it doesn't work without aggregation function applied so I tried lambda but it didn't sort correctly:

countries.sort_values(['GDP'], ascending=False).groupby(['Region','Country']).aggregate(lambda x:x)

How can I handle it?

Use DataFrame.sort_values by both columns and then convert Region and Country to MultiIndex by DataFrame.set_index :

df1 = (countries.sort_values(['Region','GDP'], ascending=[True, False])
                .set_index(['Region','Country']))
print (df1)
                     GDP
Region  Country         
Africa  Egypt         42
        Chad          30
        Kongo         28
Oceania Australia    120
        New Zealand  100
        Indonesia     50

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM