Skip to content
Advertisement

Can I avoid that the join column of the right data frame in a pandas merge appears in the output?

I am merging two data frames with pandas. I would like to avoid that, when joining, the output includes the join column of the right table.

Example:

JavaScript

df.columns will give the output Index(['Name', 'Age', 'Name_child', 'Toy'], dtype='object'). Is there an easy way to obtain Index(['Name', 'Age', 'Toy'], dtype='object') instead? I can drop the column afterwards of course like this del df['Name_child'], but I’d like my code to be as short as possible.

Advertisement

Answer

Based on @mgc comments, you don’t have to rename the columns of df2. Just you pass df2 to merge function with renamed columns. df2 column names will remain as it is.

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement