我有一个包含短语的数据帧,我只想从数据帧中提取由连字符分隔的复合词,并将它们放在另一个数据帧中。在
df=pd.DataFrame({'Phrases': ['Trail 1 Yellow-Green','Kim Jong-il was here', 'President Barack Obama', 'methyl-butane', 'Derp da-derp derp', 'Pok-e-mon'],})
到目前为止,我得到的是:
^{pr2}$结果
>>> new
part1 part2
0 Trail 1 Yellow Green
1 Kim Jong il was here
2 NaN NaN
3 methyl butane
4 Derp da derp derp
5 Pok e-mon
我想要的是这个单词应该是这样的(注意Pok-e-mon由于有两个连字符而显示为Nan
):
>>> new
part1 part2
0 Yellow Green
1 Jong il
2 NaN NaN
3 methyl butane
4 da derp
5 NaN NaN
鉴于规格,我看不出您的第一行
Nan, Nan
来自何处。可能是你的例子中的打字错误?无论如何,这里有一个可能的解决办法。在您可以使用以下正则表达式:
.
占用空间。[a-zA-Z]
只选择字母,这样可以避免从一个单词跳到另一个单词。在pok-e-mon
的情况,您需要检查是否在匹配之前或之后没有连字符。在见Demo here
相关问题 更多 >
编程相关推荐