Sort Python Data Frame (Pandas) in two levels

Asked

Viewed 11,835 times

1

I have this Dataframe and I want to organize it first by name and then by year, that is, Sort it in doís levels.

How can I do that?

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
  • What have you tried? Are you using a library?

  • Using Pandas. I know there is the function "pandas.DataFrame.sort_values()", but I want to organize in two levels: Country Name and Year.

1 answer

1


I was in doubt if I understood completely, so I created a new version of the data and includes 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 "print" 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 do the Sort on df adding the result to a new dataframe (df2)

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

Finally, we will 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

That’s it?

  • That. I wanted to sort first by names and, as a second argument, the years. You helped. Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.