2

I have an excel file with 200 rows, 2 of which have comma separated values in them. If I output them to tab-separated, it would look like this:

col1  col2    col3
a     b,c     d,e
f     g,h     i,j

I need to explode to get a dataframe like this, exploding 200 rows into ~4,000:

col1  col2  col3
a     b     d
a     b     e
a     c     d
a     c     e
f     g     i
f     g     j
f     h     i
f     h     j

I don't see any explode functionality in pandas and haven't been able to figure out how to do this having the columns of comma-separated values uneven in length - not sure how split would work here.

Help me stack-overflow, you're my only hope. Thanks!

1 Answer 1

6

Use itertools.product to get all combinations between col2 and col3, and then convert them into separate columns

from itertools import product
df.set_index('col1')\
  .apply(lambda x: pd.Series(list(product(x.col2.split(','),x.col3.split(',')))),axis=1)\
  .stack()\
  .reset_index(1,drop=True)\
  .apply(pd.Series)\
  .reset_index().rename(columns={0:'col1',1:'col3'})

Out[466]: 
  col1 col1 col3
0    a    b    d
1    a    b    e
2    a    c    d
3    a    c    e
4    f    g    i
5    f    g    j
6    f    h    i
7    f    h    j
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks @ScottBoston
I'm not going to get fired! haha. Worked like a charm on my data. Thank you, @allen & Scott so much! I need to get better with pandas and check out itertools. Much appreciated.
@Super_John You are welcome. Getting better with Pandas is why I answer question on StackOverflow. You can learn a lot from people like Allen and a few others on here.
@ScottBoston I've been stuck in SAS for the last 6 months (UGHHH), so need to invest in pandas skills to get the heck out and back in to python!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.