我有一个包含短语的数据框,我想只从数据框中提取用连字符分隔的复合词,并将它们放在另一个数据帧中。
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'],})
到目前为止,这是我到目前为止所得到的:
import pandas as pd
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'],})
new = df['Phrases'].str.extract("(?P<part1>.*?)-(?P<part2>.*)")
结果
>>> 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
我想要的是拥有这样的单词(注意由于2个连字符,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
答案 0 :(得分:1)
您可以使用此正则表达式:
(?:[^-\w]|^)(?P<part1>[a-zA-Z]+)-(?P<part2>[a-zA-Z]+)(?:[^-\w]|$)
(?: # non capturing group
[^-\w]|^ # a non-hyphen or the beginning of the string
)
(?P<part1>
[a-zA-Z]+ # at least a letter
)-(?P<part2>
[a-zA-Z]+
)
(?:[^-\w]|$) # either a non-hyphen character or the end of the string
.
占用空间。 [a-zA-Z]
只选择字母,这样可以避免从一个单词“跳”到另一个单词。pok-e-mon
案例,您需要在比赛前或比赛后检查是否有连字符。请参阅Demo here
答案 1 :(得分:1)
根据规格,我不知道你的第一行Nan, Nan
来自哪里。也许这是你的例子中的拼写错误?无论如何,这是一个可能的解决方案。
import re
# returns words with at least one hyphen
def split_phrase(phrase):
return re.findall('(\w+(?:-\w+)+)', phrase)
# get all words with hyphens
words_with_hyphens = sum(df.Phrases.apply(split_phrase).values)
# split all words into parts
split_words = [word.split('-') for word in words_with_hyphens]
# keep words with two parts only, else return (Nan, Nan)
new_data = [(ws[0], ws[1]) if len(ws) == 2 else (np.nan, np.nan) for ws in split_words]
# create the new DataFrame
pd.DataFrame(new_data, columns=['part1', 'part2'])
# part1 | part2
#------------------
# 0 Yellow | Green
# 1 Jong | il
# 2 methyl | butane
# 3 da | derp
# 4 NaN | NaN