Puneet Tripathi
Puneet Tripathi

Reputation: 422

Transposing the data in python

My Data looks something like this

cust c1 p1 c2 p2 c3 p3
1    2  3  4  5  6  7
8    9  10 11 12 13 14

I want to transpose this data and return in below format:

cust c  p
1    2  3
1    4  5
1    6  7
8    9  10
8    11 12
8    13 14

Please Can someone suggest what to use?

Upvotes: 1

Views: 114

Answers (3)

Akash Nayak
Akash Nayak

Reputation: 831

if your data in a pandas data frame. you can use this code:

import pandas as pd
df.transpose()# df is the name of data frame.

Or you can use directly:

df.T#In this case data frame name is df.T means transpose.

i think it will help you.

Upvotes: 1

Jatet
Jatet

Reputation: 9

If you get the data in a list, you can use csv.

import csv

f = open('name.csv','wb')
csv = csv.reader(f,delimiter=';')

csv.writerow([list[0]]+[list[1]]+...)

Upvotes: 1

jezrael
jezrael

Reputation: 862511

You can use function lreshape with sort_values:

print (pd.lreshape(df, {'c': ['c1', 'c2', 'c3'], 'p': ['p1', 'p2', 'p3']})
         .sort_values('cust'))

   cust   c   p
0     1   2   3
2     1   4   5
4     1   6   7
1     8   9  10
3     8  11  12
5     8  13  14

Upvotes: 2

Related Questions