0

I have multiple folders and each folder contain 4 csv files with each file containing one column of data. For each folder, I want to merge these files together in a way that new dataframe will carry 4 columns from those csv files. Next I want to copy these 4 columns into an existing txt file that already has 3 columns. So the 4 columns (from csv files) will be placed next to existing columns. This operation will be done for multiple folders. I will greatly appreciate some help.

Ali Ajaz
  • 59
  • 8
  • Hi @Ali_Ajaz, can you show us what you've tried so far? In the meantime, an easy Python tool to use for this would be [csvkit](https://csvkit.readthedocs.io/en/latest/). – sboysel Oct 22 '19 at 03:57

1 Answers1

1
  • You can use pandas for it.

You can use this link: https://stackoverflow.com/a/21232849/4561068 to make a list of ALL the csv filename and make a dataframe out of each and append of a list of dataframes, and then at the end of iterating through all of them, you can concatenate all the dataframe in that list together as shown in the link.

Afterward, you can simply write the dataframe to a txt file.

Hope that helps!!

Waqar Bin Kalim
  • 321
  • 1
  • 7
  • Hi Waqar, Thanks for replying to my question. I tried the code available in the link you mentioned. It adds columns from different files into one column. I am looking for 4 different columns in one dataframe. – Ali Ajaz Oct 22 '19 at 05:08
  • So a column for something along the lines of whether it's the first, second, third, fourth file in the folder? – Waqar Bin Kalim Oct 22 '19 at 13:04