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. 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.
- Import pandas library
- Then 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’ dataframe
- Save ‘Output_df’ in tsv file
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
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
Please Login to comment...