In this call to df.sort_index() on a MultiIndex dataframe, how to use func_2 for level two?
func_1 = lambda s: s.str.lower()
func_2 = lambda x: np.abs(x)
m_sorted = df_multi.sort_index(level=['one', 'two'], key=func_1)
The documentation says "For MultiIndex inputs, the key is applied per level", which is ambiguous.
import pandas as pd
import numpy as np
np.random.seed(3)
# Create multiIndex
choice = lambda a, n: np.random.choice(a, n, replace=True)
df_multi = pd.DataFrame({
'one': pd.Series(choice(['a', 'B', 'c'], 8)),
'two': pd.Series(choice([1, -2, 3], 8)),
'A': pd.Series(choice([2,6,9,7] ,8))
})
df_multi = df_multi.set_index(['one', 'two'])
# Sort MultiIndex
func_1 = lambda s: s.str.lower()
func_2 = lambda x: np.abs(x)
m_sorted = df_multi.sort_index(level=['one'], key=func_1)
sort_indextakes a unique function askeythat would be used for all levels.That said, you could use a wrapper function to map the desired sorting function per level name:
NB. in case of no match a default function is used that returns the level unchanged.
Another option with
numpy.lexsortinstead ofsort_index:lexsortuses the major keys last, thus the[::-1]Output: