0

I have 2 very simple addition problems with Pandas, I hope you could help me.

My first question:

Let say I have the following two dataframes: a_df and b_df

a = [[1,1,1,1],[0,0,0,0],[1,1,0,0]]
a_df = pd.DataFrame(a)

a_df = 
   0  1  2  3
0  1  1  1  1
1  0  0  0  0
2  1  1  0  0


b = [1,1,1,1]
b_df = pd.DataFrame(b).T

b_df=
   0  1  2  3
0  1  1  1  1

I would like to add b_df to a_df to obtain c_df such that my expected output would be the follow:

c_df = 
   0  1  2  3
0  2  2  2  2
1  1  1  1  1
2  2  2  1  1

The current method I use is replicate b_df to the same size of a_df and carry out the addition, shown below. However, this method is not very efficient if my a_df is very very large.

a = [[1,1,1,1],[0,0,0,0],[1,1,0,0]]
a_df = pd.DataFrame(a)

b = [1,1,1,1]
b_df = pd.DataFrame(b).T
b_df = pd.concat([b_df]*len(a_df)).reset_index(drop=True)

c_df = a_df + b_df

Are there any other ways to add b_df(without replicating it) to a_df in order to obtain what I want c_df to be?

My second question is very similar to my first one:

Let say I have d_df and e_df as follows:

d = [1,1,1,1]
d_df = pd.DataFrame(d)

d_df=
   0
0  1
1  1
2  1
3  1



e = [1]
e_df = pd.DataFrame(e)

e_df=
   0
0  1

I want to add e_df to d_df such that I would get the following result:

   0
0  2
1  2
2  2
3  2

Again, current I am replicating e_df using the following method (same as Question 1) before adding with d_df

d = [1,1,1,1]
d_df = pd.DataFrame(d)

e = [1]
e_df = pd.DataFrame(e)
e_df = pd.concat([e_df]*len(d_df)).reset_index(drop=True)

f_df = d_df + e_df

Is there a way without replicating e_df?

Please advise and help me. Thank you so much in advanced

Tommy

Yippee
  • 237
  • 1
  • 10

2 Answers2

1

For first convert one row DataFrame to Series:

c_df = a_df + b_df.iloc[0]
print (c_df)
   0  1  2  3
0  2  2  2  2
1  1  1  1  1
2  2  2  1  1

Same principe is for second:

c_df = d_df + e_df.iloc[0]
print (c_df)
   0
0  2
1  2
2  2
3  2

More information is possible find in How do I operate on a DataFrame with a Series for every column.

jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
1

Try this :

    pd.DataFrame(a_df.to_numpy() + b_df.to_numpy())


    0   1   2   3
0   2   2   2   2
1   1   1   1   1
2   2   2   1   1

numpy offers the broadcasting features that allows you to add the way u want, as long as the shape is similar on one end. I feel someone has answered something similar to this before. Once I find it I will reference it here.
This article from numpy explains broadcasting pretty well

sammywemmy
  • 27,093
  • 4
  • 17
  • 31