Home »
Python »
Python Programs
Python Pandas: Merge only certain columns
Given two Pandas DataFrames, we have to merge only certain columns.
Submitted by Pranit Sharma, on June 12, 2022
DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and the data. DataFrame can be created with the help of python dictionaries or lists but in the real world, CSV files are imported and then converted into DataFrames. Sometimes, DataFrames are first written into CSV files. Here, we are going to merge some specific columns of DataFrames.
Problem statement
Given a Pandas DataFrame, we have to merge only certain columns.
Merge only certain columns in Pandas DataFrame
To merge only certain columns, you can use pandas.DataFrame.merge() method. Also, we will pass the list of names of columns that we want to merge. When we want to update a DataFrame with the help of another DataFrame, we use this method. This method is used to merge two DataFrames based on an index.
Syntax
DataFrame.merge(
right,
how='inner',
on=None,
left_on=None,
right_on=None,
left_index=False,
right_index=False,
sort=False,
suffixes=('_x', '_y'),
copy=True,
indicator=False,
validate=None
)
Note
To work with pandas, we need to import pandas package first, below is the syntax:
import pandas as pd
Let us understand with the help of an example how to merge some specific columns into another DataFrame.
Python program to merge only certain columns
# Importing pandas package
import pandas as pd
# Creating a dataframe
df1 = pd.DataFrame({
'Name':['Ravi', 'Ram', 'Garv', 'Shivam', 'Shobhit'],
'Marks':[80, 90, 75, 88, 59]}
)
# Creating another dataframe
df2 = pd.DataFrame({
'Name':['Ravi', 'Shivam', 'Geeta', 'Garv'],
'Grade':['A', 'A', 'B', 'A'],
'Age':[30, 19, 24, 20],
'Gender':['Male', 'Female', 'Female', 'Female']}
)
# Display df1
print("DataFrame 1:\n",df1)
# Display df2
print("Datarame 2:\n",df2)
# Merging only Name and Age column of
# df2 with df1
result = df1.merge(df2[['Name','Age']])
# Display the result
print("Merged DataFrame:\n",result)
Output
Python Pandas Programs »