In this post, I am going to explain how to sort the DataFrame by a single column values.
To demonstrate the application, I am using the below data set.
Name Age City Gender 0 Krishna 34.0 Bangalore Male 1 Sailu 35.0 Hyderabad Female 2 Joel 29.0 None Male 3 Chamu NaN Chennai Female 4 Jitendra 52.0 None Male 5 Raj NaN Chennai Male
Example 1: Sort by name in ascending order.
sort_by_name_ascending_1 = df.sort_values('Name')
Above statement sort the DataFrame by ‘Name’ column and assign the result dataset to the variable ‘sort_by_name_ascending_1’. Above statement do not have an effect on the original DataFrame.
sort_by_name_ascending_1 : Name Age City Gender 3 Chamu NaN Chennai Female 4 Jitendra 52.0 None Male 2 Joel 29.0 None Male 0 Krishna 34.0 Bangalore Male 5 Raj NaN Chennai Male 1 Sailu 35.0 Hyderabad Female
We can achieve the same result with below statements too.
sort_by_name_ascending_2 = df.sort_values(by='Name')
sort_by_name_ascending_3 = df.sort_values(by='Name', ascending=True)
sort_by_name_ascending_4 = df.sort_values('Name', ascending=True)
Example 2: Sort by Name column in descending order.
‘sort_values’ method sort the DataFrame by given column in ascending order by default. You can customize the sorting order by setting the argument ‘ascending’ to False.
sort_by_name_descending_1 = df.sort_values('Name', ascending=False)
sort_by_name_descending_1 : Name Age City Gender 1 Sailu 35.0 Hyderabad Female 5 Raj NaN Chennai Male 0 Krishna 34.0 Bangalore Male 2 Joel 29.0 None Male 4 Jitendra 52.0 None Male 3 Chamu NaN Chennai Female
Find the below working application.
sort_by_name.py
import pandas as pd
import numpy as np
# Create a sample DataFrame
data = {'Name': ['Krishna', 'Sailu', 'Joel', 'Chamu', 'Jitendra', "Raj"],
'Age': [34, 35, 29, np.nan, 52, np.nan],
'City': ['Bangalore', 'Hyderabad', None, 'Chennai', None, 'Chennai'],
'Gender': ['Male', 'Female', 'Male', 'Female', 'Male', 'Male']}
df = pd.DataFrame(data)
sort_by_name_ascending_1 = df.sort_values('Name')
sort_by_name_ascending_2 = df.sort_values(by='Name')
sort_by_name_ascending_3 = df.sort_values(by='Name', ascending=True)
sort_by_name_ascending_4 = df.sort_values('Name', ascending=True)
print('df : \n', df)
print('\nsort_by_name_ascending_1 : \n', sort_by_name_ascending_1)
print('\nsort_by_name_ascending_2 : \n', sort_by_name_ascending_2)
print('\nsort_by_name_ascending_3 : \n', sort_by_name_ascending_3)
print('\nsort_by_name_ascending_4 : \n', sort_by_name_ascending_4)
print('\nSort by Name in descending order\n')
sort_by_name_descending_1 = df.sort_values('Name', ascending=False)
print('\nsort_by_name_descending_1 : \n', sort_by_name_descending_1)
Output
df : Name Age City Gender 0 Krishna 34.0 Bangalore Male 1 Sailu 35.0 Hyderabad Female 2 Joel 29.0 None Male 3 Chamu NaN Chennai Female 4 Jitendra 52.0 None Male 5 Raj NaN Chennai Male sort_by_name_ascending_1 : Name Age City Gender 3 Chamu NaN Chennai Female 4 Jitendra 52.0 None Male 2 Joel 29.0 None Male 0 Krishna 34.0 Bangalore Male 5 Raj NaN Chennai Male 1 Sailu 35.0 Hyderabad Female sort_by_name_ascending_2 : Name Age City Gender 3 Chamu NaN Chennai Female 4 Jitendra 52.0 None Male 2 Joel 29.0 None Male 0 Krishna 34.0 Bangalore Male 5 Raj NaN Chennai Male 1 Sailu 35.0 Hyderabad Female sort_by_name_ascending_3 : Name Age City Gender 3 Chamu NaN Chennai Female 4 Jitendra 52.0 None Male 2 Joel 29.0 None Male 0 Krishna 34.0 Bangalore Male 5 Raj NaN Chennai Male 1 Sailu 35.0 Hyderabad Female sort_by_name_ascending_4 : Name Age City Gender 3 Chamu NaN Chennai Female 4 Jitendra 52.0 None Male 2 Joel 29.0 None Male 0 Krishna 34.0 Bangalore Male 5 Raj NaN Chennai Male 1 Sailu 35.0 Hyderabad Female Sort by Name in descending order sort_by_name_descending_1 : Name Age City Gender 1 Sailu 35.0 Hyderabad Female 5 Raj NaN Chennai Male 0 Krishna 34.0 Bangalore Male 2 Joel 29.0 None Male 4 Jitendra 52.0 None Male 3 Chamu NaN Chennai Female sort_by_name_na_is_first : Name Age City Gender 3 Chamu NaN Chennai Female 4 Jitendra 52.0 None Male 2 Joel 29.0 None Male 0 Krishna 34.0 Bangalore Male 5 Raj NaN Chennai Male 1 Sailu 35.0 Hyderabad Female
No comments:
Post a Comment