Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting Series into DataFrame with automatic reindexing

Tags:

python

pandas

I have a DataFrame and a Series of different dimensions

from pandas import *
df = DataFrame({'a':[1,2,3],'b':[1,2,3]})
s = Series([1,2,3,4,5])

is there a way to insert s into df without creating a reindexed copy of df first? Currently i am using

df = df.reindex(range(len(s))
df['s'] = s

print df
    a   b  s
0   1   1  1
1   2   2  2
2   3   3  3
3 NaN NaN  4
4 NaN NaN  5
like image 724
greole Avatar asked May 20 '26 20:05

greole


1 Answers

Use concat:

In [19]:

concat([df,s], axis=1)

Out[19]:
    0   1  2
0   1   1  1
1   2   2  2
2   3   3  3
3 NaN NaN  4
4 NaN NaN  5
like image 64
EdChum Avatar answered May 25 '26 16:05

EdChum



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!