2

I have a table that looks like this:

Column A | Column B
      10 |      0.1
      10 |      0.2
      10 |      0.3
      20 |      0.4
      20 |      0.5
      20 |      0.6

I need to transpose it to look like this:

 10 | 20
0.1 | 0.4
0.2 | 0.5
0.3 | 0.6

Almost 2k different values in Column A, for each one there is exactly 1k values in column B, which might repeat.

Is there an automated way of doing this?

RSFalcon7
  • 202

2 Answers2

1

This python script did the trick:

import numpy as np

data = np.genfromtxt('inputcsv', delimiter=',')
keys = sorted(set(data[:,0]))

result = np.array([])

for k in keys:
    col = data[np.where(data[:,0] == k)][:,1]
    if not result.any():
        result = col
    else:
        result = np.vstack((result, col))
    print('key {0} finished'.format(k))

np.savetxt('final.csv', np.transpose(result), delimiter=',')
RSFalcon7
  • 202
0

I would bake a PHP script:

<?php
$file = fopen($argv[1], "r");
while(!feof($file)) {
    $line =  trim(fgets($file));
    if ($line=='') {continue;}
    $line = explode(' ', $line);
    $a[$line[0]] []= $line[1];
}
fclose($file);
foreach ($a as $k=>$v) {
    echo $k,"\t";
}
echo "\n";
$a = array_map(null, ...$a);
foreach($a as $b){
    echo implode("\t",$b),"\n";
}

Input file (tab-separated):

10      0.1
10      0.2
10      0.3
20      0.4
20      0.5
20      0.6
30      0.3
10      0.9

Command:

php script inputfile

Output (tab-separated):

10      20      30
0.1     0.4     0.3
0.2     0.5
0.3     0.6
0.9
simlev
  • 3,912