How to merge two csv files by specific column using Pandas in Python? Last Updated : 13 Jan, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report In this article, we are going to discuss how to merge two CSV files there is a function in pandas library pandas.merge(). Merging means nothing but combining two datasets together into one based on common attributes or column. Syntax: pandas.merge() Parameters : data1, data2: Dataframes used for merging.how: {‘left’, ‘right’, ‘outer’, ‘inner’}, default ‘inner’on: label or list Returns : A DataFrame of the two merged objects. There are 4 types of a merge.InnerLeftRightOuter We are going to use the below two csv files i.e. loan.csv and borrower.csv to perform all operations: Inner Join By setting how='inner' it will merge both dataframes based on the specified column and then return new dataframe containing only those rows that have a matching value in both original dataframes. Code: Python3 import pandas as pd # reading two csv files data1 = pd.read_csv('datasets/loan.csv') data2 = pd.read_csv('datasets/borrower.csv') # using merge function by setting how='inner' output1 = pd.merge(data1, data2, on='LOAN_NO', how='inner') # displaying result print(output1) Output: Left Outer Join By setting how='left' it will merge both dataframes based on the specified column and then return new dataframe containing all rows from left dataframe including those rows also who do not have values in the right dataframe and set right dataframe column value to NAN. Code: Python3 import pandas as pd # reading csv files data1 = pd.read_csv('datasets/loan.csv') data2 = pd.read_csv('datasets/borrower.csv') # using merge function by setting how='left' output2 = pd.merge(data1, data2, on='LOAN_NO', how='left') # displaying result print(output2) Output: Right Outer Join By setting how='right' it will merge both dataframes based on the specified column and then return new dataframe containing all rows from right dataframe including those rows also who do not have values in the left dataframe and set left dataframe column value to NAN. Code: Python3 import pandas as pd # reading csv files data1 = pd.read_csv('datasets/loan.csv') data2 = pd.read_csv('datasets/borrower.csv') # using merge function by setting how='right' output3 = pd.merge(data1, data2, on='LOAN_NO', how='right') # displaying result print(output3) Output: Full Outer Join By setting how='right' it will merge both dataframes based on the specified column and then return new dataframe containing rows from both dataframes and set NAN value for those where data is missing in one of the dataframes. Code: Python3 import pandas as pd # reading csv files data1 = pd.read_csv('datasets/loan.csv') data2 = pd.read_csv('datasets/borrower.csv') # using merge function by setting how='outer' output4 = pd.merge(data1, data2, on='LOAN_NO', how='outer') # displaying result print(output4) Output: Comment More infoAdvertise with us Next Article How to merge two csv files by specific column using Pandas in Python? P patildhanu4111999 Follow Improve Article Tags : Python Python-pandas Python pandas-io Practice Tags : python Similar Reads How to merge many TSV files by common key using Python Pandas? For data analysis the most important thing is data and we need to prepare it before we can use it for analysis. Sometimes required data can be scattered in multiple files and we need to merge them. In this article, we are going to merge multiple TSV (Tab Separated Values) files with a common key. Th 3 min read How to import excel file and find a specific column using Pandas? To read specific columns from an Excel file in Pandas, you have the flexibility to use either column indices or letters. This is achieved by setting the usecols argument, which can take a comma-separated string or a list containing column identifying letters or indices. In this article, we will lear 5 min read Reading specific columns of a CSV file using Pandas When working with large datasets stored in CSV (Comma-Separated Values) files, itâs often unnecessary to load the entire dataset into memory. Instead, you can selectively read specific columns using Pandas in Python.Read Specific Columns From CSV FileLet us see how to read specific columns of a CSV 3 min read Convert Text File to CSV using Python Pandas Converting Text File to CSV using Python Pandas refers to the process of transforming a plain text file (often with data separated by spaces, tabs, or other delimiters) into a structured CSV (Comma Separated Values) file using the Python Pandas library.In this article we will walk you through multip 2 min read How to Merge all excel files in a folder using Python? In this article, we will see how to combine all Excel files present in a folder into a single file. Module used: The python libraries used are: Pandas: Pandas is a python library developed for a python programming language for manipulating data and analyzing the data. It is widely used in Data Scien 3 min read Like