2 回答
TA贡献1789条经验 获得超8个赞
您可以使用 str.extract 使用模式提取所有匹配项,
df = df.assign(**df['Headline'].str.extract('(?P<Headline_Player>.*)\s\((?P<Location>.*)\)\s(?P<Status>.*)'))
df = df.dropna()
df['Injury/Rest'] = np.where(df['Location'].eq('rest'), 'Rest', 'Injury')
Player Name Headline Headline_Player Location Status Injury/Rest
1 LeBron James LeBron James (groin) probable for Thursday LeBron James groin probable for Thursday Injury
3 LeBron James Kyrie Irving (groin) plans to play on Tuesday Kyrie Irving groin plans to play on Tuesday Injury
4 LeBron James LeBron James (rest) questionable Tuesday LeBron James rest questionable Tuesday Rest
5 LeBron James LeBron James (leg) will start on Saturday LeBron James leg will start on Saturday Injury
编辑:要处理像 Unfortunately to hear that LeBron James (groin) probably for Thursday) 这样的边缘情况,您可以使用正则表达式提取两个由空格分隔的字符串。如果名称是两个字符串的形式,这将严格起作用。
df.assign(**df['Headline'].str.extract('(?P<Headline_Player>\w+\s\w+)\s\((?P<Location>.*)\)\s(?P<Status>.*)'))
TA贡献1878条经验 获得超4个赞
这个怎么样?
df_new = df[df.Headline.str.contains('\(')].copy()
df_new['Headline_Player'] = df_new.Headline.apply(lambda x: x.split('(')[0])
df_new['Location']=df.Headline.str.extract('\((.*)\)')[0]
df_new['Injury/Rest'] = np.where(df_new['Location'].eq('rest'), 'Rest', 'Injury')
df_new['Status'] = df_new.Headline.apply(lambda x: x.split(')')[1])
df_new
输出
Player Name Headline Headline_Player Location Injury/Rest Status
LeBron James LeBron James (groin) probable for Thursday LeBron James groin Injury probable for Thursday
LeBron James Kyrie Irving (groin) plans to play on Tuesday Kyrie Irving groin Injury plans to play on Tuesday
LeBron James LeBron James (rest) questionable Tuesday LeBron James rest Rest questionable Tuesday
LeBron James LeBron James (leg) will start on Saturday LeBron James leg Injury will start on Saturday
LeBron James Kevin Love (hip) is questionable Kevin Love hip Injury is questionable
Ryan Anderson Anderson (flu) returns against Cavs on Sunday Anderson flu Injury returns against Cavs on Sunday
Ryan Anderson Anderson (rest) not playing Anderson rest Rest not playing
添加回答
举报