I have a csv file in which the first column contains an identifier and the second column associated data. The identifier is replicated an arbitrary number of times so the file looks like this.
data1,123
data1,345
data1,432
data2,654
data2,431
data3,947
data3,673
I would like to merge the records to generate a single record for each identifier and get.
data1,123,345,432
data2,654,431
data3,947,673
Is there an efficient way to do this in python or numpy? Dictionaries appear to be out due to duplicate keys. At the moment I have the lines in a list of lists then looping through and testing for identity with the previous value at index 0 in the list but this is very clumsy. Thanks for any help.
If all the instances of a given value in the first column are consecutive, this is a perfect use case for itertools.groupby. It would be used something like this:
from itertools import groupby
from csv import reader
from operator import itemgetter
with open(filename) as f:
for k, g in groupby(reader(f), key=itemgetter(0)):
record = ','.join(k, *g)
# do something with record, e.g. write to a file
(You might have to do ','.join(k, *list(g)) or something like that, I can't test it out at the moment)
You can use a dictionary if the values are lists. defaultdict in the collections module is very useful for this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With