Home > Software design >  Conditionally replacing the value of one column with another in python
Conditionally replacing the value of one column with another in python

Time:02-26

This is more or less how one dataframe is:

ID,WinnerID
    
5863, 13463
4506, 20345
4514, 21012
4543, 20476

I have another file that has some ID's from the ID column that I would like replace with WinnerID

Grade   ID, etc.
6, 4543, bla bla bla
6, 44519, bla bla bla
6, 44483, bla bla bla
6, 5863, bla bla bla
6, 44532, bla bla bla
6, 5863, bla bla bla
6, 44496, bla bla bla
6, 4543, bla bla bla

I thought of some sort of merge? In sas I would do some logic like

if in1 and not in2 then ID = WinnerID 

during a merge but I'm not as familiar with python

I want the resulting data to be:

Grade   ID, etc.
6, 20476, bla bla bla
6, 44519, bla bla bla
6, 44483, bla bla bla
6, 13463, bla bla bla
6, 44532, bla bla bla
6, 13463, bla bla bla
6, 44496, bla bla bla
6, 20476, bla bla bla

I have seen solutions for R, and SQL but nothing for python

CodePudding user response:

Given

>>> df1 
     ID  WinnerID
0  5863     13463
1  4506     20345
2  4514     21012
3  4543     20476
>>> df2 
   Grade   Date
0      6   4543
1      6  44519
2      6  44483
3      6   5863
4      6  44532
5      6   5863
6      6  44496
7      6   4543

you can use

df2['Date'] = df2['Date'].replace(dict(df1.values))

Output:

>>> df2
   Grade   Date
0      6  20476
1      6  44519
2      6  44483
3      6  13463
4      6  44532
5      6  13463
6      6  44496
7      6  20476
  • Related