You can sort Pandas DataFrame with the following code. I highly recommend you This book to learn Python. In this article, You will see 6 examples to sort DataFrame.
- Sort Pandas DataFrame in an ascending order
- Sort DataFrame in a descending order
- Sort DataFrame by multiple columns
- Sort DataFrame by index
- Sort DataFrame and put NAs first
- Sort DataFrame by date
Step 1: Install Pandas Library
Install the Pandas library using this code, if it is not installed.
pip install pandas
Example 1: Sort DataFrame in an ascending order
# Import the Pandas library as pd
import pandas as pd
# Initialize a dictionary
dict = {'Companies':['Amazon', 'Tesla', 'Facebook', 'Google', 'Apple'],
'Stock':[113, 734, 169, 2316, 141],
'Founded':[1994, 2003, 2004, 1998, 1976],
'Worth':[420, 735, 538, 1549, 65]}
# Create DataFrame from dictionary
df = pd.DataFrame(dict)
# Display the DataFrame
print(df)
# Sorting in Ascending Order
df.sort_values(by=['Companies'], inplace=True)
# Display the DataFrame
print(df)Output:
Companies Stock Founded Worth 0 Amazon 113 1994 420 1 Tesla 734 2003 735 2 Facebook 169 2004 538 3 Google 2316 1998 1549 4 Apple 141 1976 65 Companies Stock Founded Worth 0 Amazon 113 1994 420 4 Apple 141 1976 65 2 Facebook 169 2004 538 3 Google 2316 1998 1549 1 Tesla 734 2003 735
Example 2: Sort DataFrame in a descending order
# Import the Pandas library as pd
import pandas as pd
# Initialize a dictionary
dict = {'Companies':['Amazon', 'Tesla', 'Facebook', 'Google', 'Apple'],
'Stock':[113, 734, 169, 2316, 141],
'Founded':[1994, 2003, 2004, 1998, 1976],
'Worth':[420, 735, 538, 1549, 65]}
# Create DataFrame from dictionary
df = pd.DataFrame(dict)
# Display the DataFrame
print(df)
# Sorting in Ascending Order
df.sort_values(by=['Worth'], inplace=True, ascending=False)
# Display the DataFrame
print(df)Output:
Companies Stock Founded Worth 0 Amazon 113 1994 420 1 Tesla 734 2003 735 2 Facebook 169 2004 538 3 Google 2316 1998 1549 4 Apple 141 1976 65 Companies Stock Founded Worth 3 Google 2316 1998 1549 1 Tesla 734 2003 735 2 Facebook 169 2004 538 0 Amazon 113 1994 420 4 Apple 141 1976 65
Example 3: Sort DataFrame by multiple columns
# Import the Pandas library as pd
import pandas as pd
# Initialize a dictionary
dict = {'Companies':['Amazon', 'Tesla', 'Facebook', 'Google', 'A'],
'Stock':[113, 734, 169, 2316, 141],
'Founded':[1994, 2003, 2004, 1998, 1976],
'Worth':[420, 735, 538, 1549, 65]}
# Create DataFrame from dictionary
df = pd.DataFrame(dict)
# Display the DataFrame
print(df)
# Sort by multiple columns
df.sort_values(by=['Companies','Founded'], inplace=True)
# Display the DataFrame
print(df)Output:
Companies Stock Founded Worth 0 Amazon 113 1994 420 1 Tesla 734 2003 735 2 Facebook 169 2004 538 3 Google 2316 1998 1549 4 A 141 1976 65 Companies Stock Founded Worth 4 A 141 1976 65 0 Amazon 113 1994 420 2 Facebook 169 2004 538 3 Google 2316 1998 1549 1 Tesla 734 2003 735
Example 4: Sort DataFrame by index
dict = {'Companies':['Amazon', 'Tesla', 'Facebook', 'Google', 'A'],
'Stock':[113, 734, 169, 2316, 141],
'Founded':[1994, 2003, 2004, 1998, 1976],
'Worth':[420, 735, 538, 1549, 65]}
df = pd.DataFrame(dict, index=[50, 39, 14, 5, 25])
# Display the DataFrame
print(df)
# Sort the Pandas DataFrame by Index
df1 = df.sort_index()
# Display the DataFrame
print(df1)Output:
Companies Stock Founded Worth 50 Amazon 113 1994 420 39 Tesla 734 2003 735 14 Facebook 169 2004 538 5 Google 2316 1998 1549 25 A 141 1976 65 Companies Stock Founded Worth 5 Google 2316 1998 1549 14 Facebook 169 2004 538 25 A 141 1976 65 39 Tesla 734 2003 735 50 Amazon 113 1994 420
Example 5: Sort DataFrame and put NAs first
import numpy as np
# Initialize a list of list
a = [[5, 8, 10, 49, 12],
[20, 25, 6, 13, 19],
[23, 43, 51, np.nan, 98],
[10, 15, 20, 30, 40]]
# Create DataFrame
df = pd.DataFrame(a, columns=["A", "B", "C", "D", "E"])
# Display the DataFrame
print(df)
# Sort the DataFrame and put NA first
df1 = df.sort_values(by='D', na_position='first')
# Display the DataFrame
print(df1)Output:
A B C D E
0 5 8 10 49.0 12
1 20 25 6 13.0 19
2 23 43 51 NaN 98
3 10 15 20 30.0 40
A B C D E
2 23 43 51 NaN 98
1 20 25 6 13.0 19
3 10 15 20 30.0 40
0 5 8 10 49.0 12
Example 6: Sort DataFrame by date
dict = {'Companies':['Amazon', 'Tesla', 'Facebook', 'Google', 'Apple'],
'Stock':[113, 734, 169, 2316, 141],
'Date':['1994-03-27', '2003-04-14', '2004-07-21', '1998-05-19', '1976-02-26'],
'Worth':[420, 735, 538, 1549, 65]}
df = pd.DataFrame(dict)
# Display the DataFrame
print(df)
# Sort the Pandas DataFrame by Date
df1 = df.sort_values(by='Date')
# Display the DataFrame
print(df1)Output:
Companies Stock Date Worth 0 Amazon 113 1994-03-27 420 1 Tesla 734 2003-04-14 735 2 Facebook 169 2004-07-21 538 3 Google 2316 1998-05-19 1549 4 Apple 141 1976-02-26 65 Companies Stock Date Worth 4 Apple 141 1976-02-26 65 0 Amazon 113 1994-03-27 420 3 Google 2316 1998-05-19 1549 1 Tesla 734 2003-04-14 735 2 Facebook 169 2004-07-21 538


