Akshay Hazari
Akshay Hazari

Reputation: 3267

Adding a column in dataframes based on similar columns in them

I am trying to get an output where I wish to add column d in d1 and d2 where a b c are same (like groupby).

For example

d1 = pd.DataFrame([[1,2,3,4]],columns=['a','b','c','d'])

d2 = pd.DataFrame([[1,2,3,4],[2,3,4,5]],columns=['a','b','c','d'])

then I'd like to get an output as

   a  b  c  d
0  1  2  3  8
1  2  3  4  5

Merging the two data frames and adding the resultant column d where a b c are same.

d1.add(d2) or radd gives me an aggregate of all columns

The solution should be a DataFrame which can be added again to another similarly.

Any help is appreciated.

Upvotes: 1

Views: 40

Answers (2)

duke yu
duke yu

Reputation: 89

df = pd.concat([d1, d2])
df.drop_duplicates()

   a  b  c  d
0  1  2  3  4
1  2  3  4  5

Upvotes: -1

jezrael
jezrael

Reputation: 863531

You can use set_index first:

print (d2.set_index(['a','b','c'])
         .add(d1.set_index(['a','b','c']), fill_value=0)
         .astype(int)
         .reset_index())

   a  b  c  d
0  1  2  3  8
1  2  3  4  5

Upvotes: 2

Related Questions