0

I have two dataframes, A and B.
I want to combine them by pandas.concat([A, B], axis = 1).
However, there are some duplicated columns in it as below:

# A
ID Col1 Col2 Col3 Col4 Col5 TL

# B
Col1 Col2 Col3 Col4 Col5

The value under Col1-Col5 in data B are different with the one in data A.
How to get a new dataframe with column, ID Col1 Col2 Col3 Col4 Col5 TL.
ID and TL is from data A but Col1-Col5 are from data B.

5
  • 1
    Do these DataFrames have some index that ensures alignment between them? Commented Jun 13, 2019 at 18:15
  • The order has already fixed. Just combine. Commented Jun 13, 2019 at 18:19
  • 1
    Providing example DataFrames makes it easier for ppl to answer. minimal reproducible example Commented Jun 13, 2019 at 18:32
  • da[['c1','c2']] = db[['c1','c2']]? Commented Jun 13, 2019 at 18:39
  • pd.concat([da['ID'],db[['c1','c2']], da['TL']], 1)? Commented Jun 13, 2019 at 18:43

2 Answers 2

1

Using @wwii setup. Let use combine_first:

db.combine_first(da)

Output:

  ID TL  c1  c2
0  a  q  10  20
1  b  r  11  21
2  c  s  12  22
3  d  t  13  23
4  e  u  14  24
Sign up to request clarification or add additional context in comments.

2 Comments

@PeterChan Did this solution help you? Would you consider accepting a solution?
@ScottBoston Thanks! It works and it should be a good solution!
0
da = pd.DataFrame({'ID':list('abcde'), 'c1':range(5),'c2':range(5), 'TL':list('qrstu')})
db = pd.DataFrame({'c1':range(10,15),'c2':range(20,25)})

Not clear what you need, here are a few.

da.update(db)

da[['c1','c2']] = db[['c1','c2']]

cols = [c for c in da.columns if c in db.columns]
da[cols] = db[cols]

dc = pd.concat([da['ID'],db[['c1','c2']], da['TL']], 1)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.