pandas DataFrame - Sort values in ascending or descending order

The rows of pandas DataFrame can be sorted by sort_values.

import pandas

a = [
    ('Texas', 29),
    ('New York', 19),
    ('California', 40),
    ('Florida', 21),
]

df = pandas.DataFrame(a, columns=['State', 'Population'])
df = df.sort_values('Population')

print(df)
#         State  Population
# 1    New York          19
# 3     Florida          21
# 0       Texas          29
# 2  California          40

Ascending and Descending

Sorting order depends on the option argument ascending of sort_values and it's basically a list of boolean values.

import pandas

a = [
    ('Texas', 29),
    ('New York', 19),
    ('California', 40),
    ('Florida', 21),
]

df = pandas.DataFrame(a, columns=['State', 'Population'])

df1 = df.sort_values(['Population'], ascending=[True])
df2 = df.sort_values(['Population'], ascending=[False])

print(df1)
#         State  Population
# 1    New York          19
# 3     Florida          21
# 0       Texas          29
# 2  California          40

print(df2)
#         State  Population
# 2  California          40
# 0       Texas          29
# 3     Florida          21
# 1    New York          19

pandas Series

pandas tutorial