sort_naturally(df: pandas.core.frame.DataFrame, column_name: str, **natsorted_kwargs) → pandas.core.frame.DataFrame¶
Sort a DataFrame by a column using “natural” sorting.
Natural sorting is distinct from the default lexiographical sorting provided by
pandas. For example, given the following list of items:
[“A1”, “A11”, “A3”, “A2”, “A10”]
lexicographical sorting would give us:
[“A1”, “A10”, “A11”, “A2”, “A3”]
By contrast, “natural” sorting would give us:
[“A1”, “A2”, “A3”, “A10”, “A11”]
This function thus provides “natural” sorting on a single column of a dataframe.
To accomplish this, we do a natural sort on the unique values that are present in the dataframe. Then, we reconstitute the entire dataframe in the naturally sorted order.
Natural sorting is provided by the Python package natsort.
All keyword arguments to
natsortshould be provided after the column name to sort by is provided. They are passed through to the
Functional usage syntax:
import pandas as pd import janitor as jn df = pd.DataFrame(...) df = jn.sort_naturally( df=df, column_name='alphanumeric_column', )
Method chaining usage syntax:
import pandas as pd import janitor df = pd.DataFrame(...) df = df.sort_naturally( column_name='alphanumeric_column', )
df – A pandas DataFrame.
column_name – The column on which natural sorting should take place.
natsorted_kwargs – Keyword arguments to be passed to natsort’s
A sorted pandas DataFrame.