-1

I have this list:

key,value
1,"1"
1,"2"
1,"3"
2,"1"
2,"2"
2,"3"
2,"4"
2,"5"
3,"1"
3,"2"
3,"3"

How i can merge the value based on having the same key so it become:

key,value
1,"1,2,3"
2,"1,2,3,4,5"
3,"1,2,3"

I'm using this code from Python csv: merge rows with same field but the result is:

1,"1",1,"2",1,"3"
3,"1",3,"2",3,"3"
2,"1",2,"2",2,"3",2,"4",2,"5"
Ahmad Alzoughbi
  • 474
  • 1
  • 5
  • 14

3 Answers3

1

IIUC, if you want to see the possibility of such, without a business case for it, here's what you can do with a pandas dataframe d that has the key,value:

d.groupby(['key'])['value'].apply(lambda x: ','.join(x)).reset_index()


    key value
0   1   1,2,3
1   2   1,2,3,4,5
2   3   1,2,3  
skrubber
  • 1,095
  • 1
  • 9
  • 18
0

You can try this:

s = """
key,value
1,"1"
1,"2"
1,"3"
2,"1"
2,"2"
2,"3"
2,"4"
2,"5"
3,"1"
3,"2"
3,"3"
 """
import itertools
new_data = [i.split(',') for i in s.split('\n')][2:-1]
new_data = [[int(a), b[1:-1]] for a, b in new_data]
final_data = [(a, ','.join([d for c, d in list(b)])) for a, b in itertools.groupby(new_data, key=lambda x:x[0])]

Output:

[(1, '1,2,3'), (2, '1,2,3,4,5'), (3, '1,2,3')]
Ajax1234
  • 69,937
  • 8
  • 61
  • 102
0

You can also try using defaultdict:

In [15]: li = [(1, '1'), (1, '2'), (1, '3'), (2, '1'), (2, '2'), (2, '3'), (2, '4'), (2, '5'), (3, '1'), (3, '2'), (3, '3')]

In [16]: dic = defaultdict(list)
In [17]: for key, value in li:
    ...:     dic[key].append(value)

In [18]: dic
Out[18]: 
defaultdict(list,
            {1: ['1', '2', '3'],
             2: ['1', '2', '3', '4', '5'],
             3: ['1', '2', '3']})
Ankur Ankan
  • 2,953
  • 2
  • 23
  • 38