3 回答
TA贡献1835条经验 获得超7个赞
您可以使用str.extract合适的regex. 这将找到周围的所有值:(也去除冒号周围的空格):
df[['Machine', 'Action']] = df.Description.str.extract('(.*) : (.*)',expand=True)
>>> df
Description Machine Action
0 Machine x : Turn off Machine x Turn off
1 Another action here NaN NaN
2 Another action here NaN NaN
3 Machine y : Turn off Machine y Turn off
4 Machine x : Turn on Machine x Turn on
5 Another action here NaN NaN
# df[['Machine', 'Action']] = df.Description.str.extract('(.*) : (.*)',expand=True).fillna('')
TA贡献1844条经验 获得超8个赞
只需使用split与expand=True
df[['Machine', 'Action']] =df.Description.str.split(':',expand=True).dropna()
df
Description Machine Action
0 Machine x : Turn off Machine x Turn off
1 Another action here NaN NaN
2 Another action here NaN NaN
3 Machine y : Turn off Machine y Turn off
4 Machine x : Turn on Machine x Turn on
5 Another action here NaN NaN
TA贡献1757条经验 获得超8个赞
给定一个数据框
>>> df
Description
0 Machine x : Turn off
1 Another action here
2 Another action here
3 Machine y : Turn off
4 Machine x : Turn on
5 Another action here
我会通过Series.str.split(splitter, expand=True).
>>> has_colon = df['Description'].str.contains(':')
>>> df[['Machine', 'Action']] = df.loc[has_colon, 'Description'].str.split('\s*:\s*', expand=True)
>>> df
Description Machine Action
0 Machine x : Turn off Machine x Turn off
1 Another action here NaN NaN
2 Another action here NaN NaN
3 Machine y : Turn off Machine y Turn off
4 Machine x : Turn on Machine x Turn on
5 Another action here NaN NaN
如果您更喜欢空字符串,可以NaN通过以下方式替换单元格
>>> df.fillna('')
Description Machine Action
0 Machine x : Turn off Machine x Turn off
1 Another action here
2 Another action here
3 Machine y : Turn off Machine y Turn off
4 Machine x : Turn on Machine x Turn on
5 Another action here
添加回答
举报