0

I have the following dataframes in Pandas:

df1:
index  column
1         A1
2         A2

df2:
index  column
2         A2_new
3         A3

I want to get the result:

index  column
1         A1
2         A2_new
3         A3

How do I can achieve this?

df1.update(df2) is not helpful, because I want to see row with index 3 in the result.

1
  • Does df1 = df2.reindex(df1.index.union(df2.index)).fillna(df1) work for your use case? Commented May 7, 2023 at 10:14

3 Answers 3

1

Example

df1 = pd.DataFrame(['A1', 'A2'], columns=['column'], index=[1, 2])
df2 = pd.DataFrame(['A2_new', 'A3'], columns=['column'], index=[2, 3])

df1

    column
1   A1
2   A2

df2

    column
2   A2_new
3   A3

Code

df2.combine_first(df1)

output

    column
1   A1
2   A2_new
3   A3
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, it's the most laconic way.
0

@Ars ML You can concatenate the two DataFrames vertically and remove duplicates from 'index' column, keeping only the last occurrence of each index value

df1 = pd.DataFrame({'index': [1, 2], 'column': ['A1', 'A2']})
df2 = pd.DataFrame({'index': [2, 3], 'column': ['A2_new', 'A3']})

merged_df = pd.concat([df1, df2]).drop_duplicates(subset=['index'], keep='last')
merged_df.set_index('index', inplace=True)

outputs as per your desired outcome.

1          A1
2      A2_new
3          A3

You can also use merge, it is more involved but produces your desired outcome.

merge_chain = pd.merge(df1, df2, on='index', how='outer') \
                .assign(column=lambda x: x['column_y'].fillna(x['column_x'])) \
                .drop(['column_x', 'column_y'], axis=1) \
                .set_index('index')

Comments

0

Another possible solution:

out = pd.concat([df1, df2])
out[~out.index.duplicated(keep='last')]

Output:

   column
1      A1
2  A2_new
3      A3

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.