iOSecure
iOSecure

Reputation: 232

Writing to specific column from csv file

Is it possible in Python to copy data from a separate csv and place it in a specific column of another csv?

I'd like to take only the first column of each csv file file1.csv file2.csv file3.csv

File1.csv Column A
File2.csv Column A
File3.csv Column A

and have a single output.csv with the column A data of all those csv's being written to its own column

output.csv Column A (file1)
output.csv Column B (file2)
output.csv Column C (file3)

Here is what I have for the filtering portion, not sure how to do it forwriter

 csvinputlist = ['file1.csv','file2.csv','file3.csv']


for csvinput in csvinputlist
    reader = csv.reader(open(r'{0}'.format(csvinput)), delimiter=',') 
    filtered = filter(lambda p:column A == p[0], reader)
    csv.writer(open(output.csv ???????

Upvotes: 0

Views: 2836

Answers (1)

Neill Herbst
Neill Herbst

Reputation: 2122

You can use pandas for this:

import pandas as pd
csvinputlist = ['file1.csv','file2.csv','file3.csv']
csvinput = csvinputlist
df = pd.DataFrame()
for i, csvinput in enumerate(csvinputlist):
    reader = pd.read_csv(csvinput) 
    df['A{}'.format(i+1)]=reader['A']

df.to_csv('constructed.csv')

Where 'A' represents your column header of the column you want to read. If your column do not have headers you can assign them as follows:

import pandas as pd
csvinputlist = ['file1.csv','file2.csv','file3.csv']
csvinput = csvinputlist
headers=['A', 'B', 'C'] # continue for as many column that are in the file
df = pd.DataFrame()
for i, csvinput in enumerate(csvinputlist):
    reader = pd.read_csv(csvinput, header=None, names=headers) 
    df['A{}'.format(i+1)]=reader['A']

df.to_csv('constructed.csv')

Upvotes: 2

Related Questions