2

I have a .csv file with the following format:

A   B   C   D   E   F
X1  X2  X3  X4  X5  X6
Y1  Y2  Y3  Y4  Y5  Y6
Z1  Z2  Z3  Z4  Z5  Z6

What I want:

A   X1
B   X2
C   X3
D   X4
E   X5
F   X6
A   Y1
B   Y2
C   Y3
D   Y4
E   Y5
F   Y6
A   Z1
B   Z2
C   Z3
D   Z4
E   Z5
F   Z6

I am unable to wrap my mind around the built-in transpose functions in order to achieve the final result. Any help would be appreciated.

1 Answer 1

7

You can simply melt your dataframe using pandas:

import pandas as pd

df = pd.read_csv(csv_filename)
>>> pd.melt(df)
   variable value
0         A    X1
1         A    Y1
2         A    Z1
3         B    X2
4         B    Y2
5         B    Z2
6         C    X3
7         C    Y3
8         C    Z3
9         D    X4
10        D    Y4
11        D    Z4
12        E    X5
13        E    Y5
14        E    Z5
15        F    X6
16        F    Y6
17        F    Z6

A pure python solution would be as follows:

file_out_delimiter = ','  # Use '\t' for tab delimited.
with open(filename, 'r') as f, open(filename_out, 'w') as f_out:
    headers = f.readline().split()
    for row in f:
        for pair in zip(headers, row.split()):
            f_out.write(file_out_delimiter.join(pair) + '\n')  

resulting in the following file contents:

A,X1
B,X2
C,X3
D,X4
E,X5
F,X6
A,Y1
B,Y2
C,Y3
D,Y4
E,Y5
F,Y6
A,Z1
B,Z2
C,Z3
D,Z4
E,Z5
F,Z6
Sign up to request clarification or add additional context in comments.

4 Comments

Just to confirm, the pandas melt function takes the original columns and turns them into rows?
Yes, but notice the default ordering is different in case that is important to you.
Nice solutions +1, however I'd not put an extra blank space here: f_out.write(", ".join(pair) + '\n') and maybe join them with "\t" as it looks like that's what OP has?
@AntonvBR Yes, but tabs are hard to see when displayed on SO. I've made it a little more clear.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.