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
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
ASKER
Sorry I am new to Python coding.
Is regularArray my file I am transposing?
transposedArr = zip(*regularArray)
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:
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.
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]
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.
ASKER
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
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:
Or you can change the split command to separate by space.
TXN_ID,CAT_ID
123,45
123,42
123,56
222,10
455,22
455,11
Or you can change the split command to separate by space.
More especifically, use this line:
instead of this line:
cols = filter(None,line.split(' '))
instead of this line:
cols = line.split(',')
ASKER
Hi Walter,
It worked. Just wondering how the output can be written to a file.
Thanks
It worked. Just wondering how the output can be written to a file.
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window