Home »
Python »
Python Programs
Sort dataframe by string length
Given a pandas dataframe, we have to sort dataframe by string length.
Submitted by Pranit Sharma, on October 18, 2022
Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.
The string is a group of characters, these characters may consist of all the lower case, upper case, and special characters present on the keyboard of a computer system. A string is a data type and the number of characters in a string is known as the length of the string.
Problem statement
Suppose we are given a DataFrame with a string type column and we need to sort this by the length of the string. It means the shortest string will be stacked on the top and the longest string will be stacked at the bottom.
Sorting dataframe by string length
We will simply use the str.len() method to find out the length and then apply the sort_values() method to sort these values.
Let us understand with the help of an example,
Python program to sort DataFrame by string length
# Importing pandas package
import pandas as pd
# Creating a dictionary
d = {
'Name':['Ram','Raghavendra','Shantanu'],
'Age':[20,21,22]
}
# Creating a DataFrame
df = pd.DataFrame(d)
# Display original DataFrame
print("Original Dataframe:\n",df,"\n")
# Sorting by string length
res = df.Name.str.len().sort_values().index
# Display Modified DataFrame
print('Modified DataFrame:\n',df.reindex(res))
Output
The output of the above program is:
Python Pandas Programs »