Let’s create some dummy data:
import pandas as pd df=pd.DataFrame({"text":['aa','bbb','c','dd']}) df
Using the “key” variable inside the sort_values function, we can use a lambda expression to sort our data frame by string length.
df.sort_values('text',key=lambda x:x.str.len())