Dervin Thunk
Dervin Thunk

Reputation: 20139

How do I replace pandas rows with values of another dataframe for all instances of the value in the first df?

I have two dataframes:

df1=
A   B   C
a   1   3
b   2   3
c   2   2
a   1   4
df2=
A   B   C
a   1   3.5

Now I need to replace all occurrences of a in df1 (2 in this case) with a in df2, leaving b and c unchanged. The final dataframe should be:

df_final=
A   B   C
b   2   3
c   2   2
a   1   3.5

Upvotes: 0

Views: 49

Answers (2)

Quang Hoang
Quang Hoang

Reputation: 150785

Do you mean:

df_final = pd.concat((df1[df1['A'].ne('a')], df2))

Or if you have several values like a:

list_special = ['a']
df_final = pd.concat((df1[~df1['A'].isin(list_special)], df2))

Upvotes: 1

Randy
Randy

Reputation: 14847

If df2 just has the average of duplicated values, you can do df1.groupby(["A", "B"]).mean().reset_index()

Otherwise, you can do something like this:

In [27]: df = df1.groupby(["A", "B"]).first().merge(df2, how="left", on=["A", "
    ...: B"])
    ...: df["C"] = df["C_y"].fillna(df["C_x"])
    ...: df = df[["A", "B", "C"]]
    ...: df
Out[27]:
   A  B    C
0  a  1  3.5
1  b  2  3.0
2  c  2  2.0

Upvotes: 0

Related Questions