merge - Python Pandas: how to overwrite subset of a dataframe with a subset of another dataframe? -
given df1 , df2, how df3 using pandas, df3 has df1 elements:
[11, 12, 21, 22] in place of df2 elements
[22, 23, 32, 33] condition: indexes of row 1 & 2 in df1 same indexes of row 2 & 3 in df2
you looking dataframe.loc method
small example:
import pandas pd df1 = pd.dataframe({"data":[1,2,3,4,5]}) df2 = pd.dataframe({"data":[11,12,13,14,15]}) df3 = df1.copy() df3.loc[3:4] = df2.loc[3:4] df3 data 0 1 1 2 2 3 3 14 4 15
Comments
Post a Comment