how to sort by index python code example Example 1: pandas sort dataframe by index df.sort_index() Example 2: how to sort index pd.sort_index()