Link to home
Start Free TrialLog in
Avatar of Ricky Ng
Ricky Ng

asked on

what is the python code to transpose rows of data to columns separated by commas?

Hi Experts,

I have these fields:

TXN_ID     CAT_ID
123              45
123              42
123              56
222              10
455              22
455              11


I want to transpose to this without column headers using Python code

123, 45, 42, 56
222, 10
455, 22, 11

Please help?

Thanks
Avatar of Walter Ritzel
Walter Ritzel
Flag of Brazil image

In theory, this is your code:
transposedArr = zip(*regularArray)

Open in new window

Avatar of Ricky Ng
Ricky Ng

ASKER

Sorry I am new to Python coding.

Is regularArray my file I am transposing?

transposedArr = zip(*regularArray)
Sorry, I did not understand that you were using a file from your question.

Your case is a little more complicated, because you are not simply transposing, you are agregating the information. zip(* will turn rows into columns and vice versa, but without the summarization you are requesting.
To do that, the simplest way is to do this:
filename = 'Q_28491431.txt'  #this a file containing the initial lines you had posted.
f = open(filename,'rt')
dataAgg = {}
for line in f.readlines():
	cols = line.split(',') #I have assumed this is a csv file, using , to separate columns
	if cols[0] in dataAgg.keys():
		dataAgg[cols[0]].append(cols[1]) 
	else:
		arr = []
		arr.append(cols[1])
		dataAgg[cols[0]] = arr

for item in dataAgg.keys():
	print item,',',dataAgg[item]

Open in new window


Please observe that this may not be the most "pythonic" way of doing it, but I think it is very clear if you understand other programming languages. If you want to write "pythonic" code, using what is state of art in python code, you need to read documentation and lots of books.
Hi Walter,

Thanks for your reply. I am getting this error when I run your code.

Traceback (most recent call last):
  File "test.py", line 10, in <module>
    arr.append(cols[1])
IndexError: list index out of range
hpc2879@qsbcomp1$

Thanks
your file entry should be like this to work:
TXN_ID,CAT_ID
123,45
123,42
123,56
222,10
455,22
455,11

Open in new window


Or you can change the split command to separate by space.
More especifically, use this line:
 
	cols = filter(None,line.split(' '))

Open in new window


instead of this line:
cols = line.split(',')

Open in new window

Hi Walter,

It worked. Just wondering how the output can be written to a file.

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Walter Ritzel
Walter Ritzel
Flag of Brazil image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial