Maulik Upadhyay
Maulik Upadhyay

Reputation: 127

transposing dataframe pandas

I have following file format:

SA BTSA01_U01 0 0 0 -9 G G T T    
SA BTSA01_U02 0 0 0 -9 G G T T

want to transpose it using pandas, following is the code I tried:

import pandas as pd from pandas import DataFrame 
  def transpose(file1,file2):
  source=pd.read_csv(file1,sep=None,engine='python')
  dest=source.transpose()
  dest.to_csv(file2)

But it did not give me the desire output,following is the output:

0    
SA  SA    
BTSA01_U01  BTSA01_U02    
0   0    
0.1 0   
0.2 0    
-9  -9    
G   G    
G.1 G    
T   T    
T.1 T    

I tried using some of the options like, header=False, index=False with dest.to_csv, but none is working, I am wondering about addition of "." and "1" in some of the values in first column,

Upvotes: 1

Views: 716

Answers (1)

EdChum
EdChum

Reputation: 393893

You didn't specify header=None so your first line is being interpreted as column names but this will generate duplicate names which isn't allowed so you get .1 appended.

So you need:

source=pd.read_csv(file1,sep=None,engine='python', header=None)

Upvotes: 1

Related Questions