How to transpose a dataset in a csv file?

If the whole file contents fits into memory, you can use import csv from itertools import izip a = izip(*csv.reader(open(“input.csv”, “rb”))) csv.writer(open(“output.csv”, “wb”)).writerows(a) You can basically think of zip() and izip() as transpose operations: a = [(1, 2, 3), (4, 5, 6), (7, 8, 9)] zip(*a) # [(1, 4, 7), # (2, 5, 8), # … Read more

Transpose 2d array, join second level with commas, and join first level with pipes

I’m assuming that you have this array: $array = array ( array (’01’,’03’,’02’,’15’), array (’05’,’04’,’06’,’10’), array (’07’,’09’,’08’,’11’), array (’12’,’14’,’13’,’16’) ); In which case, you can do this: $tmpArr = array(); foreach ($array as $sub) { $tmpArr[] = implode(‘,’, $sub); } $result = implode(‘|’, $tmpArr); echo $result; See it working

Restructure multidimensional array of column data into multidimensional array of row data [duplicate]

As Kris Roofe stated in his deleted answer, array_column is indeed a more elegant way. Just be sure to put it into some kind of a foreach loop, similar to what Sahil Gulati showed you. For example, like this: $result = array(); foreach($where[‘id’] as $k => $v) { $result[] = array_column($where, $k); } The var_dump … Read more