Normally merge

When we join a dataset using pd.merge() function with type ‘inner’, the output will have prefix and suffix attached to the identical columns on two data frames, as shown in the output.

Python3




# import python pandas package
import pandas as pd
 
# import the numpy package
import numpy as np
 
# Create sample dataframe data1 and data2
data1 = pd.DataFrame(np.random.randint(1000, size=(1000, 3)),
                     columns=['EMI', 'Salary', 'Debt'])
data2 = pd.DataFrame(np.random.randint(1000, size=(1000, 3)),
                     columns=['Salary', 'Debt', 'Bonus'])
 
# Merge the DataFrames
merged = pd.merge(data1, data2, how='inner', left_index=True,
                  right_index=True)
print(merged)


Output:

Prevent duplicated columns when joining two Pandas DataFrames

Column duplication usually occurs when the two data frames have columns with the same name and when the columns are not used in the JOIN statement. In this article, let us discuss the three different methods in which we can prevent duplication of columns when joining two data frames.

Syntax: pandas.merge(left, right, how=’inner’, on=None, left_on=None, right_on=None)

Explanation:

  • left – Dataframe which has to be joined from left
  • right – Dataframe which has to be joined from the right
  • how – specifies the type of join. left, right, outer, inner, cross
  • on – Column names to join the two dataframes.
  • left_on – Column names to join on in the left DataFrame.
  • right_on – Column names to join on in the right DataFrame.

Similar Reads

Normally merge

When we join a dataset using pd.merge() function with type ‘inner’, the output will have prefix and suffix attached to the identical columns on two data frames, as shown in the output....

Use the columns that have the same names in the join statement

...

Preventing duplicates by mentioning explicit suffix names for columns

In this approach to prevent duplicated columns from joining the two data frames, the user needs simply needs to use the pd.merge() function and pass its parameters as they join it using the inner join and the column names that are to be joined on from left and right data frames in python....

Remove the duplicate columns before merging two columns

...

Contact Us