0

Currently attempting this with Pandas, but if there is a better way I'm all ears. I have a csv file that looks something like this:

column1 column2
value value
value value1, value2
value value

What I would like to do is:

  1. Check column2 for rows that have more than one value (e.g. "value, value").
  2. Create a new row with value2 and remove it from it's original row:
column1 column2
value value
value value1
value value2
value value

I'm not sure what is the best way to determine if a cell in column2 has 2 values, maybe something like this:

for i in df.column2:
    if "," in i:

But I'm not sure how to capture that row and write it.

rootoor
  • 25
  • 6
  • Does this answer your question? [Python pandas: explode multiple rows](https://stackoverflow.com/questions/61844022/python-pandas-explode-multiple-rows) – SomeDude Jul 14 '22 at 22:03
  • 2
    Also if you don't get it from the duplicate that I posted, you could do: `df.assign(column2=df['column2'].str.split(',')).explode('column2')` – SomeDude Jul 14 '22 at 22:03
  • @SomeDude that ended up working great, thank you – rootoor Jul 19 '22 at 00:34

0 Answers0