Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple way to append a pandas series with same index

Tags:

python

pandas

Is there a simple way to append a pandas series to another and update its index ? Currently i have two series

from numpy.random import randn
from pandas import Series

a = Series(randn(5))
b = Series(randn(5))

and i can append b to a by

a.append(b)

>>> 0   -0.191924
    1    0.577687
    2    0.332826
    3   -0.975678
    4   -1.536626
    0    0.828700
    1    0.636592
    2    0.376864
    3    0.890187
    4    0.226657

but is there a smarter way to make sure that i have a continuous index than

a=Series(randn(5))
b=Series(randn(5),index=range(len(a),len(a)+len(b)))
a.append(b)  
like image 271
greole Avatar asked Dec 20 '22 20:12

greole


2 Answers

One option is to use reset_index:

>>> a.append(b).reset_index(drop=True)
0   -0.370406
1    0.963356
2   -0.147239
3   -0.468802
4    0.057374
5   -1.113767
6    1.255247
7    1.207368
8   -0.460326
9   -0.685425
dtype: 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
like image 182
alko Avatar answered Jan 07 '23 23:01

alko


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
like image 36
behzad.nouri Avatar answered Jan 07 '23 23:01

behzad.nouri