Skip to content Skip to sidebar Skip to footer

Splitting Strings In A Column Into Multiple Rows

I have the following dataframe: d = {'col1':['a','b','c'],'col2':['x1;x2;x3','x5','x6'],'col3':['y1','y2,y3,y4','y5']} df = pd.DataFrame(d) I want to choose a column and split tha

Solution 1:

df=df.replace(regex=r'[^\w]',value=',')#replace any punctuation with comma
df=df.assign(col2=df['col2'].str.split(',')).explode('col2')#Explode column2

Post a Comment for "Splitting Strings In A Column Into Multiple Rows"