Whatever strategy you choose, according to your expectation, you will get an output file that is near 5 times bigger than your input file.
input file :
rows = 400k
columns = 3000
Total amount of datas =
400k x 3000 = 1,2 billions
output file :
rows = (400k -1) x ( 3000 -3 )
"-1" because of the header row
"-3" because the 3 first contains the datas you want
columns = 5 (i take into account the datas, not the formatting and "__" stuff)
Total amount of datas =
5 x (400k -1) x ( 3000 -3 ) =
5,99...billions
Indeed, since you will be repeating the datas of the first 3 column for each and every subsequent column. (so (3 + 2 ) x (3000 - 3) ) rather than having it once for all of them ( "+ 2" is because you even want to add the header and the values of the subsequent column).
So just because of your prerequisits and expectations, for sure you will have to write more data, and thus you will need the corresponding number of I/O writing, independently of the strategy you choose.
If you have a huge amount of data to write, then there is and uncompressible amount of time to do it.
Of course I/O operations are quicker to do in RAM or SSD than on a standard hard drive, but still ...
It would be cheaper to save the transposed matrix and request on it
So you will have it twice : in line and in columns ... of course it will cost you 1,2 billions of datas more.
But
1,2 billions x 2 = 2,4 billions is still more than
twice smaller than 5,9 billions of datas !
Anyway, processing such an amount of data using files is inappropriate : this is what
Database have been designed for...
My 2 cents ...
PS// The reality might even be worse since i didn't even take into account the size of the datas, just the number of them ...