Reputation: 584
Suppose I have a Series like this:
In [19]: sr
Out[19]:
a 1
b 2
c 3
d 4
dtype: int64
In [20]: sr.index
Out[20]: Index([u'a', u'b', u'c', u'd'], dtype='object')
Instead of sorting lexicographically, I would like to sort this series based on a custom order, say, cdab
. How can I do that?
What if it is a DataFrame; how can I sort it by a given index list?
Upvotes: 5
Views: 5430
Reputation: 176938
You can do this in number of different ways. For Series objects, you can simply pass your preferred order for the index like this:
>>> sr[['c','d','a','b']]
c 3
d 4
a 1
b 2
dtype: int64
Alternatively, both Series and DataFrame objects have a reindex
method. This allows you more flexibility when sorting the index. For instance, you can insert new values into the index (and even choose what value it should have):
>>> sr.reindex(['c','d','a','b','e'])
c 3
d 4
a 1
b 2
e NaN # <-- new index location 'e' is filled with NaN
dtype: int64
Yet another option for both Series and DataFrame objects is the ever-useful loc
method of accessing index labels:
>>> sr.loc[['c','d','a','b']]
c 3
d 4
a 1
b 2
dtype: int64
Upvotes: 8
Reputation: 20507
Just use reindex
, for example:
In [3]: sr.reindex(['c', 'd', 'a', 'b'])
Out[3]:
c 3
d 4
a 1
b 2
dtype: int64
Upvotes: 2