Combine multiple CSV files by column, not by row

2

I have ~100 CSV files that I want to combine into one single Excel spreadsheet (everything in one tab, not in separate tabs). These CSV files are identical in format: each contains 4,000 rows and 2 columns with header, about 60 KB in size.

All solutions I've found so far append the next CSV at the end of the last row of the active tab. I haven't seen one that can append new CSVs to the columns immediate to the right of last column.

Here're some examples.

  1. DOS copy method: copy *.csv merge.txt

  2. This solution. (VBA script.)

  3. Using Excel's Data>New Query>From File>From Folder (Excel 2013) .

There are also examples with Windows Powershell scripts to merge CSV files on the internet.

All of the above create a single Excel spreadsheet with about 400,000 rows of data, which is useless to me.

I'd appreciate suggestions to solve this problem. Thanks!

Edit. An easy solution is found: using r's cbind() to combine the data in a data frame and then write it to a csv. The entire process took ~3 seconds. Right tool for the job!

Thanks to everyone for contributing. Cheers,

--tcollar

tcollar

Posted 2019-09-23T23:18:40.007

Reputation: 21

tcollar - If I had time to play with it more I would to confirm the exact syntax and to keep it as simple as possible logic wise but I don't right now. Look over this post and play with some of these variations of the PowerShell logic.... https://social.technet.microsoft.com/Forums/scriptcenter/en-US/ebadf6e6-2efc-461f-8aca-fe1fba7bd1cc/unix-pasting-in-powershell?forum=ITCG

– Pimp Juice IT – 2019-09-24T01:34:02.367

I haven't seen one that can append new CSVs to the columns immediate to the right of last column. Consolidate data in multiple worksheets – Akina – 2019-09-24T05:11:37.373

Your issue is contradicts your need,,, you got solution and unable to alter it as your need,,, I think your basic need is append CSV file data side by side !! – Rajesh S – 2019-09-24T07:18:41.900

This VBA code works,, nextColumn = .Range("A1").SpecialCells(xlCellTypeLastCell).Column + 1 – Rajesh S – 2019-09-24T08:04:26.063

tcollar - Go ahead and add your answer as an actual answer with examples, detail, reference, and so forth to ensure it's clear what answer worked for you. Yes, you are just fine to add an answer to your own question, so go for it if you are saying you found an answer yourself that worked. – Pimp Juice IT – 2019-09-24T23:13:43.460

Answers

1

Not sure about "native" Windows 10, but if you have the Windows Subsystem for Linux installed, you can use the UNIX paste command, which should concatenate the files by columns, in the way you are asking.

Scot

Posted 2019-09-23T23:18:40.007

Reputation: 238

1In addition to paste the OP may want to read the man page for join as well. – ivanivan – 2019-09-24T00:03:48.620

Neither is possible. The CSV files are business documents. Can't be transferred outside the work. I'm using Linux Mint at home. – tcollar – 2019-09-24T00:17:58.853

@tcollar Too bad. To be clear, Windows Subsystem for Linux installs onto Windows 10. It's not possible to use this within your business environment?

– Scot – 2019-09-24T00:24:15.833

I know. I've checked. It is not installed for sure. I suppose I can ask for it. But it's going to be a pain. – tcollar – 2019-09-24T00:29:33.590

0

Using your third option: 'Using Excel's Data>New Query>From File>From Folder (Excel 2013)'

Click on the transform data option, this opens the power query editor. It will show a list of the files.

Click on the button in the header of the column content to create a function that can transform every individual CSV file before appending them.

Select the right table or worksheet and do the following in the transform sample file function.

  1. Insert the column names into the first row by using `use headers as first row'
  2. Transpose Table

Then in the query where the function is invoked, the transposed CSV files are appended. Simply transpose the table back to original shape and promote the first row to headers.

Burd

Posted 2019-09-23T23:18:40.007

Reputation: 36

I tried it. It sorta worked, but on my computer it took more than 10 minutes just to combine the transposed data sets! – tcollar – 2019-09-24T23:00:20.207

Glad to hear that it worked – Burd – 2019-09-25T05:29:00.490

0

Here's the r code to combine csv files by column:

setwd("C:/mycsvdir")
filenames <- list.files(full.name=TRUE)

## read csv, skipping the first two rows of every file
Everything <- lapply(filenames, function(i){read.csv(i, header=TRUE, skip=2)}
combined.df <- do.call(cbind.data.frame, Everything)

write.csv(combined.df, file = "combined.csv", row.names=FALSE)

If you want to combine csv files by row, just replace cbind with rbind.

Edit: About the sequence of merging: my csv files are named like mycsv 001.csv, mycsv 002.csv, etc. They were read in and merged exactly in that sequence, which was great for my purpose.

tcollar

Posted 2019-09-23T23:18:40.007

Reputation: 21