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?
What have you tried? Are you using a library?
– Woss
Using Pandas. I know there is the function "pandas.DataFrame.sort_values()", but I want to organize in two levels: Country Name and Year.
– Arduin