Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fill missing value based on value from another column in the same row

Tags:

python

pandas

I have a DataFrame looks like this

ColA | ColB | ColC | ColD |
-----|------|------|------|
100  |   A  |  X1  |  NaN |
200  |   B  |  X2  |  AAA |
300  |   C  |  X3  |  NaN |

I want to fill the missing value on ColD based on value on ColA. The result I need is like:

if value in ColA = 100 then value in ColD = "BBB"
if value in ColA = 300 then value in ColD = "CCC"

ColA | ColB | ColC | ColD |
-----|------|------|------|
100  |   A  |  X1  |  BBB |
200  |   B  |  X2  |  AAA |
300  |   C  |  X3  |  CCC |
like image 630
Pete Populii Avatar asked Feb 26 '17 16:02

Pete Populii


1 Answers

You can use combine_first or fillna:

df.ColD = df.ColD.combine_first(df.ColA)
print (df)
   ColA ColB ColC ColD
0   100    A   X1  100
1   200    B   X2  AAA
2   300    C   X3  300

Or:

df.ColD = df.ColD.fillna(df.ColA)
print (df)
   ColA ColB ColC ColD
0   100    A   X1  100
1   200    B   X2  AAA
2   300    C   X3  300

EDIT: First use map for Series s and then combine_first or fillna by this Series:

d = {100: "BBB", 300:'CCC'}
s = df.ColA.map(d)
print (s)
0    BBB
1    NaN
2    CCC
Name: ColA, dtype: object

df.ColD = df.ColD.combine_first(s)
print (df)
   ColA ColB ColC ColD
0   100    A   X1  BBB
1   200    B   X2  AAA
2   300    C   X3  CCC

It replace only NaN:

print (df)
   ColA ColB ColC ColD
0   100    A   X1  EEE <- changed value to EEE
1   200    B   X2  AAA
2   300    C   X3  NaN

d = {100: "BBB", 300:'CCC'}
s = df.ColA.map(d)
df.ColD = df.ColD.combine_first(s)
print (df)
   ColA ColB ColC ColD
0   100    A   X1  EEE
1   200    B   X2  AAA
2   300    C   X3  CCC
like image 174
jezrael Avatar answered Oct 23 '22 00:10

jezrael