0

I have one CSV file in which one row have lots of value which have been separated by comma. I want to separate them into different rows. Can you please help me how to do this?

example:

Product_name       brand_strength 
STELARA            90MG,130MG,45MG,5MG/1ML

Desired Output:

Product_name      brand_strength
STELARA           90MG
STELARA           130MG
STELARA           45MG
STELARA           5MG/1ML
jay
  • 5
  • 3
  • What is the separator between the values in the csv? e.g between "STELARA" and "90MG..." – Aryerez Dec 09 '19 at 06:58
  • refer [here](https://stackoverflow.com/questions/12680754/split-explode-pandas-dataframe-string-entry-to-separate-rows), if you are using pandas version >= 25 ,use `pandas.DataFrame.explode` – Shijith Dec 09 '19 at 07:02

2 Answers2

0

Try this. Split your data first and stack them to rows.

df = pd.DataFrame({"Product_name": ["STELARA"],
                  "brand_strength": ["90MG,130MG,45MG,5MG/1ML"]})

df["brand_strength"] = df["brand_strength"].str.split(",")

df = (df
 .set_index(["Product_name"])['brand_strength']
 .apply(pd.Series)
 .stack()
 .reset_index()
 .drop('level_1', axis=1)
 .rename(columns={0:'brand_strength'}))

output :

    Product_name   brand_strength
0   STELARA        90MG
1   STELARA        130MG
2   STELARA        45MG
3   STELARA        5MG/1ML
Iron Hand Odin
  • 410
  • 4
  • 10
0

This will make changes to your CSV and then write those to a new CSV file.

import pandas as pd

df = pd.read_csv('<your csv file path'>')

df.assign(brand_strength=df['brand_strength'].str.split(',')).explode('brand_strength')

df.to_csv('<new file name/path>')
Himanshu
  • 666
  • 1
  • 8
  • 18