Concatenate Two Columns in Csv: Python

How to concatenate two columns and update the csv in python?

So every row you're reading from (and writing to) csv is a list, right? So take another step or two and create the list you want, then write it.

e.g.

import csv
with open('test.csv') as f:
reader = csv.reader(f)
with open('output.csv', 'w') as g:
writer = csv.writer(g)
for row in reader:
new_row = [' '.join([row[0], row[1]])] + row[2:]
writer.writerow(new_row)

Also, I doubt you need to read/write binary ('rb', 'wb') from a csv.

Concatenate two columns in CSV: Python

import pandas as pd
df = pd.read_csv(path)
df['Arrival'] = df.CityArrival + ' ' + df.CountryArrival

Concatenate two column in python

Try this:

import csv

file1=open('Source.csv','r')
readfile=csv.reader(file1,delimiter=';')
file2=open('Output.csv','w',newline='')
writefile=csv.writer(file2,delimiter=';')
result=()
for row in readfile:
if (row[2]>condition1 and 'string' in row[6]):
result=[str(row[2])+'- '+str(row[6])]
print(result)#line just to see to info from the output file
writefile.writerow(result)

file1.close()
file2.close()

concat two columns and store in a new column in a csv

If you can use Pandas:

import pandas as pd

df = pd.read_csv('rosary.csv')
df['Image'] = df.ImageName + "_" + df.ImageWChain
df.to_csv('rosary.csv')

>>> df
Unnamed: 0 Description ImageName ImageWChain ItemNumber Keyword UPC Image
0 0 Test1 B001 B001 B001 XYZ 123 B001_B001
1 1 Test2 B002 B002 B002 GDH 456 B002_B002
2 2 Test3 B003 B003 B003 GFR 789 B003_B003

To make a new image copy:

for old_image, new_image in zip(df.ImageName, df.Image):
if (os.path.isfile(old_image)):
shutil.copy2(old_image, new_image)

CSV module to concatenate fields

When specifying that the solution must be done using the built in CSV module or a specific tool, you should consider why.

From the docs:

The csv module implements classes to read and write tabular data in CSV format...

The csv module’s reader and writer objects read and write sequences. Programmers can also read and write data in dictionary form using the DictReader and DictWriter classes.

This doesn't sound like this fits in with this use case of merging CSVs.

If this is done to familiarise yourself with the csv module alone, then consider choosing another use case to work on.


To elaborate on this point and answer the question with the specified criteria, I have done it in two ways:

  1. Forced solution using CSV module

Assuming CSV A -> one.csv and CSV B -> two.csv and CSV C -> out.csv

import csv

def get_csv_lines(filename):
"""Read csv using csv.DictReader and return
the lines as a list of dictionaries."""
lines = []
with open(filename, newline='') as csvfile:
csv_reader = csv.DictReader(csvfile)
for row in csv_reader:
lines.append(row)
return lines

def write_csv_lines(filename, lines):
"""Take a list of dictionaries and write to CSV file.
Keys are headers, values are row values."""
with open(filename, mode='w', newline='') as csv_file:
fieldnames = lines[0].keys()
writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
writer.writeheader()
for line in lines:
writer.writerow(line)

if __name__ == '__main__':
l1 = get_csv_lines('one.csv')
l2 = get_csv_lines('two.csv')
l3 = []
for num, dic1 in enumerate(l1): # foreign key first
l3.append(dic1)
for dic2 in l2: # primary key second
if dic1['ID'] == dic2['ID']:
l3[num].update(dic2) # cannot guarantee order so would have to do some more trickery if needed
write_csv_lines('out.csv', l3)

This is also based on this specific use case where one csv has a foreign key of ID and the other csv has a primary key of ID.


  1. The ease of Pandas library
import pandas as pd

df1 = pd.read_csv('one.csv')
df2 = pd.read_csv('two.csv')
df3 = pd.merge(left=df1, right=df2, how='inner', on='ID')
df3.to_csv('out.csv', index=False)

Exactly the use case Pandas merge method was built for.

How do I append two columns from csv file in list?

Instead of:

    l1.append((df['unions'][i], df['district'][i]))

Try:

    l1.append(' '.join((df['unions'][i], df['district'][i])))

Or:

    l1.append((df['unions'][i] + " " + df['district'][i]))


Related Topics



Leave a reply



Submit