Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate combinations for a comma separated strings in a pandas row

Tags:

python

pandas

I have a dataframe like this:

ID, Values
1   10, 11, 12, 13
2   14
3   15, 16, 17, 18

I want to create a new dataframe like this:

ID COl1 Col2
1  10   11
1  11   12
1  12   13
2  14
3  15   16
3  16   17
3  17   18

Please help me in how to do this??? Note: The rows in Values column of input df are str type.

like image 519
user3288743 Avatar asked Dec 02 '25 13:12

user3288743


1 Answers

Use list comprehension with flattening and small change - if i > 0: to if i == 2: for correct working with one element values:

from collections import deque

#https://stackoverflow.com/a/36586925
def chunks(iterable, chunk_size=2, overlap=1):
    # we'll use a deque to hold the values because it automatically
    # discards any extraneous elements if it grows too large
    if chunk_size < 1:
        raise Exception("chunk size too small")
    if overlap >= chunk_size:
        raise Exception("overlap too large")
    queue = deque(maxlen=chunk_size)
    it = iter(iterable)
    i = 0
    try:
        # start by filling the queue with the first group
        for i in range(chunk_size):
            queue.append(next(it))
        while True:
            yield tuple(queue)
            # after yielding a chunk, get enough elements for the next chunk
            for i in range(chunk_size - overlap):
                queue.append(next(it))
    except StopIteration:
        # if the iterator is exhausted, yield any remaining elements
        i += overlap
        if i == 2:
            yield tuple(queue)[-i:]

L = [[x] + list(z) for x, y in zip(df['ID'], df['Values']) for z in (chunks(y.split(', ')))]

df = pd.DataFrame(L, columns=['ID','Col1','Col2']).fillna('')
print (df)
   ID Col1 Col2
0   1   10   11
1   1   11   12
2   1   12   13
3   2   14     
4   3   15   16
5   3   16   17
6   3   17   18
like image 69
jezrael Avatar answered Dec 04 '25 02:12

jezrael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!