Question

Lets say I have a CSV file which looks like:

  A B C D
1 f g h j
2 s x c d
3 c f t y
4 f u i p

I would like to be able to concatenate the column rows so that I have lists which looks like:

 fscf (column A)
 gxfu (column B)
 hcti (column C)
 jdyp (column D)

I do not which to include the column headers. How can I do this?

Edit: I have the CSV file loaded into the program already.

Was it helpful?

Solution

import csv
rows = csv.reader(open('foo.csv', 'rU'),delimiter=',')
#merged_col1 = merged_col2 = merged_col3 = merged_col4 = []
headers = True
'''
my foo.csv:
HEADER1,HEADER2,HEADER3,HEADER4
a,b,c,d
e,f,g,h
i,j,k,l
m,n,o,p
'''
merged_col1 = []
merged_col2 = []
merged_col3 = []
merged_col4 = []
for row in rows:
    if headers:    #skip headers
        headers = False
        continue
    merged_col1.append(row[0])
    merged_col2.append(row[1])
    merged_col3.append(row[2])
    merged_col4.append(row[3])

print ''.join(merged_col1)
print ''.join(merged_col2)
print ''.join(merged_col3)
print ''.join(merged_col4)


OUTPUT:
-------
aeim
bfjn
cgko
dhlp

OTHER TIPS

import pandas as pd
df = pd.read_csv('filename.csv')
answer = [''.join(df.values[:,i]) for i in range(len(df.columns))]

If you do this kind of data manipulation a lot, then pandas is going to change your life.

Just read all the rows, and "join" them together

with open("input.txt","rb") as myfile:
     reader = csv.reader(myfile,delimiter=" ")
     master_row = ["","","",""]
     for row in reader:
         master_row = [master_row[col] + row[col] for col in range(0,len(master_row))]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top