2

This is a follow-up on this question

I have two dataframes that I want to merge, but I want to avoid to have duplicate columns, so I'm doing:

cols_to_use = df2.columns-df1.columns

If I print cols_to_use I get this:

 Index([col1,col2,col3...],dtype=object)

However, I have one column that I need it to be kept in both dfs, it is the co_code. That's because I'm going to merge on that column.

My question is: how to add one extra column to cols_to_use? I need it to look like this:

Index([co_code,col1,col2,col3...],dtype=object)

I tried different synthaxes but nothing seemed to work:

cols_to_use = df2.columns-df1.columns+'co_code'
cols_to_use = df2.columns-df1.columns+['co_code']
cols_to_use = df2.columns-df1.columns+df2['co_code'].columns

2 Answers 2

2
cols_to_use = df2.columns - df1.columns.difference(['co_code'])

Or,

cols_to_use = (df2.columns - df1.columns).tolist() + ['co_code']
Sign up to request clarification or add additional context in comments.

Comments

2

Similar to @COLDSPEED's solution:

cols_to_use = df2.columns.difference(df1.columns.drop('co_code'))

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.