我有一个df
,其中有一个名为Description
的列,其值如下:
ID Description
1 (a) this is good (b) bad (c) average
2 Ok
3 i am rahul works on (a) stack overflow (b) stack exchange
预期DF:
ID Description
1 (a) this is good
1 (b) bad
1 (c) average
2 Ok
3 i am rahul works on (a) stack overflow
3 (b) stack exchange
答案 0 :(得分:5)
这是一种根据您的链接之一改编的方法:
>>> df['Description'].str.split(r'.(?=\(.\))',expand = True).stack()
ID
1 0 (a) this is good
1 (b) bad
2 (c) average
2 0 Ok
3 0 i am rahul works on
1 (a) stack overflow
2 (b) stack exchange
只需使用lookahead
正则表达式进行分割即可。 3d id上的拆分有些不同,因为您并没有真正拆分。