Split words from datraframe by space to rows while duplicating the info from other columns ( python,pandas)

Viewed 182

enter image description hereI have a df which is composed by 5 columns one being a comment column where people leave a comment.

What I would like to do is split that comment column by space into multiple rows while duplicating the other columns:

df:

r_id start comments
1 2021-01-01 i am the text that needs splitting by space to rows
2 2021-01-02 hello hello

wanted result:

r_id start comments
1 2021-01-01 i
1 2021-01-01 am
1 2021-01-01 the
1 2021-01-01 text
2 2021-01-02 hello
2 2021-01-02 hello

I've tried anything from str.split() to regex without result.

-- le with code:

df = df.apply(lambda x: x.str.lower() if x.dtype == "object" else x) 
(df
 .assign(comments=df['comments'].str.split())
 .explode('comments')
)
print(df)

df['comments'] = df['comments'].str.replace('ă','a')
df['comments'] = df['comments'].str.replace('â','a')
df['comments'] = df['comments'].str.replace('î','i')
df['comments'] = df['comments'].str.replace('ș','s')
df['comments'] = df['comments'].str.replace('ț','t')
df.replace('[^a-zA-Z0-9]', ' ',regex=True)
df.dropna(inplace=True)
print(df)

yet it wouldn't split the comment

1 Answers

You need to split and explode:

df2 = (df
 .assign(comments=df['comments'].str.split())
 .explode('comments')
)

output:

   r_id        start    comments
0      1  2021-01-01           i
0      1  2021-01-01          am
0      1  2021-01-01         the
0      1  2021-01-01        text
0      1  2021-01-01        that
0      1  2021-01-01       needs
0      1  2021-01-01   splitting
0      1  2021-01-01          by
0      1  2021-01-01       space
0      1  2021-01-01          to
0      1  2021-01-01        rows
1      2  2021-01-02       hello
1      2  2021-01-02       hello
Related