Reputation: 1293
I have a demo code here to describe the requirement:
df = pd.DataFrame({'Item':['Item1','Item2','Item3'],'Des':['Welcome\nto use Python','Hello','Python\nis\ngood']})
print(df)
output:
Item Des
0 Item1 Welcome\nto use Python
1 Item2 Hello
2 Item3 Python\nis\ngood
You can see below that Welcome\nto use Python
is split into 2 phases(words) with\n
, but Welcome
and to use Python
are still in the same row.
Item Des
0 Item1 Welcome
to use Python
1 Item2 Hello
2 Item3 Python
is
good
That's the requirement. I can split the cell value to a list but have no idea about the next steps. Could you please give some suggestions on how to implement it with Pandas? Thanks.
df['Des_'] = df['Des'].apply(lambda x: x.split('\n'))
When I save it to excel, it should be like:
Upvotes: 0
Views: 362
Reputation: 153460
IIUC,
df.assign(Des=df['Des'].str.split('\n')).explode('Des')
Output:
Item Des
0 Item1 Welcome
0 Item1 to use Python
1 Item2 Hello
2 Item3 Python
2 Item3 is
2 Item3 good
Upvotes: 1