Reputation: 3275
I have a Python series that looks like:
Good 4
Satisfactory 9
Strong 57
Total 72
Weak 2
How can I move the "Total 72" to the end?
It might have been different i.e. it might have been
Good 4
Total 72
Satisfactory 9
Strong 57
Weak 2
so I want to be able to move the rows with index "Total" to the end. How can I do that?
Upvotes: 1
Views: 441
Reputation: 862661
Use Series.reindex
by sorted index by keys for test if equal Total
, it also working if no value Total
, but not working if duplicated values in index:
s1 = s.reindex(sorted(s.index, key='Total'.__eq__))
print (s1)
Good 4
Satisfactory 9
Strong 57
Weak 2
Total 72
Name: a, dtype: int64
EDIT: If possible some duplicated values is possible compare values of index, get positions of mask by Index.argsort
and last change order by positions by Series.iloc
:
print (s)
Good 4
Satisfactory 9
Strong 57
Total 72
Satisfactory 2
Name: a, dtype: int64
s1 = s.iloc[(s.index =='Total').argsort()]
print (s1)
Good 4
Satisfactory 9
Strong 57
Satisfactory 2
Total 72
Name: a, dtype: int64
Upvotes: 2