Sort Python Data Frame (Pandas) on two levels

1

I have this DataFrame and I want to organize it first by name and then by year, that is, sort it in two levels.

How can I do this?

Country Name Country Code     Indicator Name Indicator Code   Ano    Pobreza
0        Aruba          ABW  Population, total    SP.POP.TOTL  1960    54211.0
1  Afghanistan          AFG  Population, total    SP.POP.TOTL  1960  8996351.0
2       Angola          AGO  Population, total    SP.POP.TOTL  1960  5643182.0
3      Albania          ALB  Population, total    SP.POP.TOTL  1960  1608800.0
4      Andorra          AND  Population, total    SP.POP.TOTL  1960    13411.0
    
asked by anonymous 17.08.2017 / 22:39

1 answer

1

I was in doubt if I understood completely, so I created a new version of the data and included 3 times Albania with different years to check the result.

import pandas as pd
import io
# Simulando um CSV
s = '''
"Country Name","Country Code","Ano","Pobreza"
"Aruba","ABW","1960",54211
"Afghanistan","AFG","1960",8996351
"Albania","ALB","2017",5643182
"Albania","ALB","1970",1608800
"Andorra","AND","1960",13411
"Albania","ALB","1900",2588
"Angola","AGO","1966",2588
'''
# read csv
df = pd.read_csv(io.StringIO(s), usecols=['Country Name', 'Country Code','Ano',
        'Pobreza'])

Now let's "start" the dataframe:

print(df)
  Country Name Country Code   Ano  Pobreza
0        Aruba          ABW  1960    54211
1  Afghanistan          AFG  1960  8996351
2       Angola          AGO  1966  5643182
3      Albania          ALB  1970  1608800
4      Andorra          AND  1960    13411
5      Albania          ALB  1900     2588
6      Albania          ALB  2017     2588

Now let's sort in df by typing the result into a new dataframe ( df2 )

df2 = df.sort_values(['Country Name', 'Ano'])

Finally, let's print the result:

print(df2)
  Country Name Country Code   Ano  Pobreza
1  Afghanistan          AFG  1960  8996351
5      Albania          ALB  1900     2588
3      Albania          ALB  1970  1608800
2      Albania          ALB  2017  5643182
4      Andorra          AND  1960    13411
6       Angola          AGO  1966     2588
0        Aruba          ABW  1960    54211

Is that it?

    
17.08.2017 / 23:50