Pandas - Sort DataFrame by Column - Examples
Pandas - Sort DataFrame by Column
To sort a DataFrame by a specific column in Python pandas, you can use pandas.DataFrame.sort_values() method. Call the sort_values() method on the DataFrame object, and pass the required column as string to the by named parameter.
dataframe.sort_values(by='column_name')
The sort_values() method does not modify the original DataFrame, but sorts the contents of given DataFrame in ascending order and returns a new sorted DataFrame.
You can sort the DataFrame in ascending or descending order by the specified column. To sort in descending order, specify ascending=False to the sort_values() method.
dataframe.sort_values(by='column_name', ascending=False)
In this tutorial, we shall go through some example programs, where we shall sort DataFrame by a specific column in ascending or descending order.
Video Tutorial
Examples
1. Sort DataFrame by specific column in ascending order
The default sorting order of sort_values() function is ascending order.
In this example, we shall take a DataFrame in df_1 with two columns: name and quantity. We have to sort the rows in the DataFrame by the column quantity, in ascending order.
- Given a DataFrame in df_1 with two columns name and quantity.
- Call sort_values() method on df_1, and pass the column name 'quantity' for the by named parameter.
- The sort_values() method returns a new DataFrame with the contents of original DataFrame sorted in ascending order by the specified column.
- Assign the returned value to a variable, say sorted_df, and print it to output.
Python Program
import pandas as pd
# Take a DataFrame
df_1 = pd.DataFrame({
'name': ['apple', 'banana', 'cherry', 'fig'],
'quantity': [68, 74, 83, 45]
})
# Sort DataFrame by column
sorted_df = df_1.sort_values(by='quantity')
print(sorted_df)
Output
name quantity
3 fig 45
0 apple 68
1 banana 74
2 cherry 83
The rows of the DataFrame are sorted based on the increasing order of the specified column quantity.
2. Sort DataFrame by a column in descending order
To sort the dataframe in descending order a column, pass ascending=False argument to the sort_values() method.
In this example, we have to sort the DataFrame in df_1 in descending order by the column quantity.
- Given a DataFrame in df_1 with two columns name and quantity.
- Call sort_values() method on df_1, and pass the column name 'quantity' for the by named parameter, and False for the ascending named parameter.
- The sort_values() method returns a new DataFrame with the contents of original DataFrame sorted in descending order by the specified column.
- Assign the returned value to a variable, say sorted_df.
Python Program
import pandas as pd
# Take a DataFrame
df_1 = pd.DataFrame({
'name': ['apple', 'banana', 'cherry', 'fig'],
'quantity': [68, 74, 83, 45]
})
# Sort DataFrame by column in descending order
sorted_df = df_1.sort_values(by='quantity', ascending=False)
print(sorted_df)
Output
name quantity
2 cherry 83
1 banana 74
0 apple 68
3 fig 45
The rows of the returned DataFrame are sorted in descending order by the column quantity.
Summary
In this Pandas Tutorial, we learned how to sort a DataFrame by a specific column in ascending or descending order, using sort_values() method of the DataFrame, with the help of well detailed Python example programs.