0

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
0

2 Answers 2

1

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))
Sign up to request clarification or add additional context in comments.

2 Comments

Hi. Thanks. Not exactly, there are several values like 'a'.
This matches your expected output with the sample data. Maybe you want to update them to reflect what you mean. Also see updates.
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.