Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

append same Series to data frame columns

I have the following dataframe:

data=pd.DataFrame(data=[[8,4,2,6,0],[3,4,5,6,7]],columns=["a","b","c","d","e"])

Output is like this:

    a b c d e
0   8 4 2 6 0
1   3 4 5 6 7

I also have the following Series:

a=pd.Series([3,4])

I want to attach the series (a) to each of the columns in data. I tried few things with concat but I never seem to get it right.

Expected result is:

    a b c d e
0   8 4 2 6 0
1   3 4 5 6 7
2   3 3 3 3 3
3   4 4 4 4 4 

Thanks in advance

like image 350
Martin Yordanov Georgiev Avatar asked Mar 18 '26 05:03

Martin Yordanov Georgiev


2 Answers

You can do:

out=data.append(pd.concat([a]*data.shape[1],axis=1,keys=data.columns),ignore_index=True)

   a  b  c  d  e
0  8  4  2  6  0
1  3  4  5  6  7
2  3  3  3  3  3
3  4  4  4  4  4
like image 81
anky Avatar answered Mar 22 '26 15:03

anky


Here is a method from for loop

for x ,y in a.iteritems(): 
    data.loc[data.index[-1]+x+1]=y

data
Out[106]: 
   a  b  c  d  e
0  8  4  2  6  0
1  3  4  5  6  7
2  3  3  3  3  3
4  4  4  4  4  4
like image 43
BENY Avatar answered Mar 22 '26 14:03

BENY



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!