+4 votes
in Programming Languages by (14.6k points)

I concatenated two pandas series; the resulting series shows the index of the old series. How can I clear and reset the index from the old series?

E.g. The below example shows 0,1,2,0,1,2 as indices. I want 0,1,2,3,4,5 as indices.

>>> s1 = pd.Series([1, 2, 3])

>>> s2 = pd.Series([11, 12, 13])

>>> pd.concat([s1,s2])

0     1

1     2

2     3

0    11

1    12

2    13

1 Answer

+1 vote
by (48.7k points)

The concat() function has a parameter "ignore_index", which can be used to clear and reset the index. Its default value is False. That's why you got indexes from the old series. If you set it to True, the resulting axis will be labeled 0, …, n - 1.

Here is an example to reset the index.

>>> import pandas as pd
>>> s1 = pd.Series([1, 2, 3])
>>> s2 = pd.Series([11, 12, 13])
>>> pd.concat([s1,s2], ignore_index=True)
0     1
1     2
2     3
3    11
4    12
5    13
 


...