Sort Pandas DataFrame with Examples
Dataframes values can be sorted by index and values . You can use the pandas dataframe sort_values() function to sort a dataframe.
sort_values(by, axis=0, ascending=True,na_position='first', kind='quicksort')
The sort_values() method allows the flexibility to sort a dataframe by one or more columns, ascending or descending, choose the sorting algorithm, treat NaNs during sorting, using a custom key for sorting, etc.
df.sort_values(by=["Name"])
Above code sorting by "Name" column in default ascending order.
Lets' create a DataFrame...
import pandas as pd
import numpy as np
df = pd.DataFrame()
df['Name'] = ['John', 'Doe', 'Bill','Jim','Harry','Ben']
df['TotalMarks'] = [82, 38, 63,22,55,40]
df['Grade'] = ['A', 'E', 'B','E','C','D']
df['Promoted'] = [True, False,True,False,True,True]
df
Name TotalMarks Grade Promoted
0 John 82 A True
1 Doe 38 E False
2 Bill 63 B True
3 Jim 22 E False
4 Harry 55 C True
5 Ben 40 D True
Sort by single column
df.sort_values(by=["Name"])
Name TotalMarks Grade Promoted
5 Ben 40 D True
2 Bill 63 B True
1 Doe 38 E False
4 Harry 55 C True
3 Jim 22 E False
0 John 82 A True
Sort by two columns
df.sort_values(by=["TotalMarks","Name"])
Sort by column in descending order
By default DataFrame is sorted by ascending order, if you want to sort in descending order you have to set the ascending=False inside the sort_values() method.
df.sort_values(by=["TotalMarks"],ascending=False)
Name TotalMarks Grade Promoted
0 John 82 A True
2 Bill 63 B True
4 Harry 55 C True
5 Ben 40 D True
1 Doe 38 E False
3 Jim 22 E False
Sort by missing value
df = pd.DataFrame({'x':[1.0, np.NaN, 3.0, 4.0]})
df
x
0 1.0
1 NaN
2 3.0
3 4.0
Sort by missing value first/last
df.sort_values(by=["x"],na_position='first')
x
1 NaN
0 1.0
2 3.0
3 4.0
Also, you can sort by missing value last using the following method.
df.sort_values(by=["x"],na_position='last')
x
0 1.0
2 3.0
3 4.0
1 NaN
Related Topics
- Pandas DataFrame: GroupBy Examples
- Pandas DataFrame Aggregation and Grouping
- Pandas DataFrame: query() function
- Finding and removing duplicate rows in Pandas DataFrame
- How to Replace NaN Values With Zeros in Pandas DataFrame
- How to read CSV File using Pandas DataFrame.read_csv()
- How to Convert Pandas DataFrame to NumPy Array
- How to shuffle a DataFrame rows
- Import multiple csv files into one pandas DataFrame
- Create new column in DataFrame based on the existing columns
- New Pandas dataframe column based on if-else condition
- How to Convert a Dictionary to Pandas DataFrame
- Rename Pandas columns/index names (labels)