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 |
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With