How To Sort Row Index Case-insensitive Way In Pandas Dataframe
I have the following data: Set Coolthing Route Organ Up Down set4 Pam3CSK4 ID LL 81 60 set4 Poly_IC ID LL 542 92 set4 Poly_IC ID MM 73 73 set4 cdiGMP ID MM 143 78 set4 Poly_IC ID B
Solution 1:
Building on @Marius case_insensitive_order
, a single liner using reindex
In [63]: df.reindex(sorted(df.index, key=lambda x: x.lower()))
Out[63]:
Up Down
BB LL MM BB LL MM
cdiGMP 900143129078
Pam3CSK4 08100600
Poly_IC 3254273829273
Solution 2:
You can force this by using the new CategoricalIndex
(new in 0.16.1, I think), but I'm not sure if it's a great idea as it may have unpredictable effects:
case_insenstive_order = sorted(df.index, key=lambda x: x.lower())
case_insenstive_order
Out[4]: ['cdiGMP', 'Pam3CSK4', 'Poly_IC']
df.index = pd.CategoricalIndex(df.index,
categories=case_insenstive_order,
ordered=True)
df.sort_index()
Out[7]:
Up Down
BB LL MM BB LL MM
cdiGMP 900143129078
Pam3CSK4 08100600
Poly_IC 3254273829273
Solution 3:
I consider this a valid answer too:
df = df.iloc[df.index.str.lower().argsort()]
However, reindex
certainly works a bit faster:
%timeit df.reindex(sorted(df.index, key=lambda x: x.lower()), copy=True)
1000 loops, best of3: 794 µs per loop
%timeit df.iloc[df.index.str.lower().argsort()]
1000 loops, best of3: 850 µs per loop
I tested here with pandas 0.20.3 and python2 on a table that had 500 rows and about 50 columns.
Post a Comment for "How To Sort Row Index Case-insensitive Way In Pandas Dataframe"