Sorting base on the row index or column labels
index_sorted_df = df.sort_index()
Unlike sort_values this will perform sorting based on the row index or column names and not by their values
When you perform sorting, filtering or drop rows the
index label get messed up. You can sort based on this index numbers or labels
with sort_index()
Rember: By default the result of
sort_index
returns a copy of the original dataframe after being sort, the original dataframe is unafected
After applying sorting the original index labels may have become out of order.
You can sort them back with sort_index()
. By default it applies ascending
sorting
sorted_df = df.sort_values(by=["make", "model"])
sorted_df.sort_index(ascending=True)
You can sort your colummns with the axis="columns"
argument, so that they
will be in ascending or descending order based on their label names.
You can also use the
columns
name or the number1
df.sort_index(axis="columns", ascending=False)
df.sort_index(axis=1, ascending=False) # Same result as before