Simple way to append a pandas series with same index Simple way to append a pandas series with same index pandas pandas

Simple way to append a pandas series with same index


One option is to use reset_index:

>>> a.append(b).reset_index(drop=True)0   -0.3704061    0.9633562   -0.1472393   -0.4688024    0.0573745   -1.1137676    1.2552477    1.2073688   -0.4603269   -0.685425dtype: float64

For the sake of justice, Roman Pekar method is fastest:

>>> timeit('from __main__ import np, pd, a, b; pd.Series(np.concatenate([a,b]))', number = 10000)0.6133969540821536>>> timeit('from __main__ import np, pd, a, b; pd.concat([a, b], ignore_index=True)', number = 10000)1.020389742271714>>> timeit('from __main__ import np, pd, a, b; a.append(b).reset_index(drop=True)', number = 10000)2.2282133623128075


you can also use concat with ignore_index=True: (see docs )

pd.concat([a, b], ignore_index=True)

edit: my tests with larger a and b:

a = pd.Series(pd.np.random.randn(100000))b = pd.Series(pd.np.random.randn(100000))%timeit pd.Series(np.concatenate([a,b]))1000 loops, best of 3: 1.05 ms per loop%timeit pd.concat([a, b], ignore_index=True)1000 loops, best of 3: 1.07 ms per loop%timeit a.append(b).reset_index(drop=True)100 loops, best of 3: 5.11 ms per loop


I think @runnerup answer is the way to go, but you can also create new Series explicitly:

>>> pd.Series(np.concatenate([a,b]))0   -0.2004031   -0.9212152   -1.3388543    1.0939294   -0.8795715   -0.8103336    1.6540757    0.3609788   -0.4724079    0.123393dtype: float64