'How to replace part of dataframe in pandas

I have sample dataframe like this

df1=

A B C
a 1 2
b 3 4
b 5 6 
c 7 8
d 9 10

I would like to replace a part of this dataframe (col A=a and b) with this dataframe

df2=

A B C
b 9 10
b 11 12
c 13 14

I would like to get result below

df3=

A B C
a 1 2
b 9 10
b 11 12
c 13 14
d 9 10

I tried

df1[df1.A.isin("bc")]...

But I couldnt figure out how to replace. someone tell how to replace dataframe.



Solution 1:[1]

As I explained try update.

import pandas as pd


df1 = pd.DataFrame({"A":['a','b','b','c'], "B":[1,2,4,6], "C":[3,2,1,0]})

df2 = pd.DataFrame({"A":['b','b','c'], "B":[100,400,300], "C":[39,29,100]}).set_index(df1.loc[df1.A.isin(df2.A),:].index)
    
df1.update(df2)

Out[75]: 
   A      B      C
0  a    1.0    3.0
1  b  100.0   39.0
2  b  400.0   29.0
3  c  300.0  100.0

Solution 2:[2]

you could solve your problem with the following:

import pandas as pd

df1 = pd.DataFrame({'A':['a','b','b','c','d'],'B':[1,3,5,7,9],'C':[2,4,6,8,10]})

df2 = pd.DataFrame({'A':['b','b','c'],'B':[9,11,13],'C':[10,12,14]}).set_index(df1.loc[df1.A.isin(df2.A),:].index)

df1.loc[df1.A.isin(df2.A), ['B', 'C']] = df2[['B', 'C']]

Out[108]: 
   A   B   C
0  a   1   2
1  b   9  10
2  b  11  12
3  c  13  14
4  d   9  10

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 xxx
Solution 2 MSN