Dugong
Dugong

Reputation: 77

Compare columns from different Pandas dataframes, and replace its values <Pandas, Python>

I have a two similar data frames (named dfA, dfB) that has ID and code. ID represents patient ID, and code is disease code. dfA is larger and all dfB are actually in dfA. However, the disease code in dfA are somewhat old, and needs to be updated by code in dfB.

My task is comparing all ID row of dfA with dfB, and if there're matching ID, changing the value of disease code in dfA with dfB. So that the code is properly updated. Final result is the list of dfA (with updated code).

For regular python, it would look like this:

for i in dfA.id:
    if i == dfB.id:
        replace dfA.code with dfB.code 
    else: pass 
print(dfA)

I know Pandas loop has different rules, and one can process this without making a loop. I've done something like this, but does not work.

dfA.where(dfA.id == dfB.id), 'code'][dfB.code]

Could you shed some lights on this? Thank you

Upvotes: 0

Views: 55

Answers (1)

BENY
BENY

Reputation: 323226

Using

s=dfA.id.map(dfB.set_index('id').code)
dfA.code=s.fillna(dfA.code)

Upvotes: 2

Related Questions