3

Suppose I have a Pandas series

>>> s = pd.Series([1,2,3])
0 | 1
1 | 2
2 | 3

I can change the index by

>>> s.index = ['a', 'b', 'c']
a | 1
b | 2
c | 3

but how do I do this while chaining, like

s.apply(some_fun).<reindex to letters>.combine(...).etc.

2 Answers 2

2

Use Series.set_axis:

s = pd.Series([1,2,3])
s = s.set_axis(['a', 'b', 'c'])

print (s)
a    1
b    2
c    3
dtype: int64
1

There's a rename:

new_idx = ['a','b','c']
s.rename(dict(zip(s.index, new_idx)))

Out:

a    1
b    2
c    3
dtype: int64

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.