I 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