I am trying to replace values on df with values from another df
First DataFrame:
col1 col2 col3 col4 col5 type
0 NaN 4 7 14.0 0 B
1 3.0 2 6 11.0 5 B
2 4.0 4 0 22.0 7 C
3 5.0 6 11 8.0 3 A
4 9.0 0 3 6.0 8 B
5 2.0 1 6 NaN 2 A
6 6.0 5 7 9.0 9 B
Second DataFrame:
col1 col2
0 1 111
1 2 222
2 3 333
3 4 444
My code is:
for i in df2["col1"]:
new_value=df2.loc[df2["col1"]==i]["col2"]
df=df.replace(i,new_value)
print(df)
But the values of DataFrame haven't changed.
This is what I expect to get in the end:
col1 col2 col3 col4 col5 type
0 NaN 444 7 14.0 0 B
1 333.0 222 6 11.0 5 B
2 444.0 4414 0 22.0 7 C
3 5.0 6 11 8.0 333 A
4 9.0 0 3 6.0 8 B
5 222.0 111 6 NaN 222 A
6 6.0 5 7 9.0 9 B
I think you want:
df1.replace(df2.set_index('col1')['col2'])
Output:
col1 col2 col3 col4 col5 type
0 NaN 444 7 14.0 0 B
1 333.0 222 6 11.0 5 B
2 444.0 444 0 22.0 7 C
3 5.0 6 11 8.0 333 A
4 9.0 0 333 6.0 8 B
5 222.0 111 6 NaN 222 A
6 6.0 5 7 9.0 9 B
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