How to merge many TSV files by common key using Python Pandas? Last Updated : 23 Mar, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report 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. This can be possible by using the merge method of the pandas Python library. This method allows us to combine files by using a common key. Approach:Import pandas libraryThen read first two tsv files and merge them using pd.merge() function by setting the 'on' parameter to the common column present in both files. Then store the result in a new dataframe called 'Output_df'.Store remaining files in a list.Run a loop that will iterate over these file names. Read these files one by one and merge them with 'Output_df' dataframeSave 'Output_df' in tsv fileExample 1: In this example, we will merge tsv files by using an inner join. We have taken four tsv files for this example as follows. Used file: Customer.tsv, Account.tsv, Branch.tsv, Loan.tsv Python3 # Import pandas library import pandas as pd # Read first two csv files with '\t' separator tsv1 = pd.read_csv("Documents/Customer.tsv", sep='\t') tsv2 = pd.read_csv("Documents/Account.tsv", sep='\t') # store the result in Output_df dataframe. # Here common column is 'ID' column Output_df = pd.merge(tsv1, tsv2, on='ID', how='inner') # store remaining file names in list tsv_files = ["Branch.tsv", "Loan.tsv"] # One by one read tsv files and merge with # 'Output_df' dataframe and again store # the final result in Output_df for i in tsv_files: path = "Documents/"+i tsv = pd.read_csv(path, sep='\t') Output_df = pd.merge(Output_df, tsv, on='ID', how='inner') # Now store the 'Output_df' # in tsv file 'Output.tsv' Output_df.to_csv("Documents/Output.tsv", sep="\t", header=True, index=False) Output: Output.tsvExample 2: In this example, we will merge tsv files by using an outer join. We have taken four tsv files for this example as follows. Used file: Course.tsv, Teacher.tsv, Credits.tsv, Marks.tsv Python3 # Import pandas library import pandas as pd # Read first two csv files with '\t' separator tsv3 = pd.read_csv("Documents/Course.tsv", sep='\t') tsv4 = pd.read_csv("Documents/Teacher.tsv", sep='\t') # store the result in Output_df dataframe. # Here common column is 'Course_ID' column Output_df2 = pd.merge(tsv3, tsv4, on='Course_ID', how='outer') # store remaining file names in list tsv_files = ["Credits.tsv", "Marks.tsv"] # One by one read tsv files and merge with # 'Output_df2' dataframe and again store # the final result in 'Output_df2' for i in tsv_files: path = "Documents/"+i tsv = pd.read_csv(path, sep='\t') Output_df2 = pd.merge(Output_df2, tsv, on='Course_ID', how='outer') # Now store the 'Output_df2' in tsv file 'Output_outer.tsv' # Here we replacing nan values with NA Output_df2.to_csv("Documents/Output_outer.tsv", sep="\t", header=True, index=False, na_rep="NA") Output: Comment More infoAdvertise with us Next Article How to merge many TSV files by common key using Python Pandas? P patildhanu4111999 Follow Improve Article Tags : Python Python-pandas Python pandas-io Practice Tags : python Similar Reads How to merge two csv files by specific column using Pandas in Python? 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 mer 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 How to write Pandas DataFrame as TSV using Python? In this article, we will discuss how to write pandas dataframe as TSV using Python. Let's start by creating a data frame. It can be done by importing an existing file, but for simplicity, we will create our own. Python3 # importing the module import pandas as pd # creating some sample data sample = 1 min read How to merge multiple folders into one folder using Python ? In this article, we will discuss how to move multiple folders into one folder. This can be done using Python's OS and Shutil module. Approach:Get the current directory and the list of the folders you want to merge.Loop through the list of folders and store their content in a list. Here, we have stor 4 min read How to Join Pandas DataFrames using Merge? Joining and merging DataFrames is that the core process to start  out with data analysis and machine learning tasks. It's one of the toolkits which each Data Analyst or Data Scientist should master because in most cases data comes from multiple sources and files. In this tutorial, you'll how to join 3 min read Like