I have an exel file named "123.csv" that is the output i get when running a PROM functionality, consisting of two columns "case" and "event". I want to modify this output by grouping events based on case. More specifically i want to write a python script that will group events that belong to the same case to be merged in a new cell, no mate what the length of my initial matrix is.Could anyone please give me some idea?
curent and desiret output
import csv
with open('123.csv', 'rb') as csvfile:
spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
for row in spamreader:
print ', '.join(row).replace(',',' ').replace('"',' ')
this is a part i wrote, but it only reads the file and removes some punctiation
解决方案
It's easy to do with simple csv & defaultdict (python 3)
Your input is like
case,event
101,A
101,X
101,Y
102,B
102,C
103,Z
code:
import collections
with open("csv.csv") as f:
cr = csv.reader(f,delimiter=",")
d=collections.defaultdict(lambda : list())
header=next(cr) # read title
for r in cr:
d[r[0]].append(r[1]) # fill dict
with open("csv2.csv","w",newline="") as f:
cr = csv.writer(f,delimiter=",")
cr.writerow(header) # title
for k,v in d.items():
cr.writerow([k,",".join(v)])
output
case,event
103,Z
101,"A,X,Y"
102,"B,C"