0

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
a     b,c
d     e,f,g

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

col1  col2
a     b
a     c
d     e
d     f
d     g

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

1 Answer 1

2

Let's use pd.DataFrame, .str.split, stack:

df_out = (pd.DataFrame(df.col2.str.split(',').tolist(), index=df.col1)
      .stack()
      .reset_index()
      .drop('level_1',axis=1)
      .rename(columns={0:'col2'}))

Output:

  col1 col2
0    a    b
1    a    c
2    d    e
3    d    f
4    d    g
Sign up to request clarification or add additional context in comments.

3 Comments

incredible. mind blown. Thank you! I have 2 csv columns - how would I add a second split column to get all combinations of col1 , col2 (csv), col3(csv)?
sure, no problem. I'll comment a link here once it's posted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.