从字符串中提取首尾两个单词作为pandas中的新列 [英] Extract first and last words from strings as a new column in pandas
本文介绍了从字符串中提取首尾两个单词作为pandas中的新列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在努力根据另一列中的字符串创建两个新列.
I am struggling to create two new columns based on string in another column.
我有什么
Profile
0 Technician
1 Service Engineer
2 Sales and Service Support Engineer
我想拥有的东西
First Last
0 Technician NaN
1 Service Engineer
2 Sales Engineer
我的尝试是使用
new = tl['Profile'].str.split(' ')
tl['First'] = new[0]
tl['Last'] = new[1]
但这仅适用于First.
But this is correct only for First.
推荐答案
在这里尝试str.extract
:
df['Profile'].str.extract(r'^(?P<First>\S+).*?(?P<Last>\S+)?$')
First Last
0 Technician NaN
1 Service Engineer
2 Sales Engineer
很少有str
方法会如此优雅,因为额外的需要仅处理一个单词的句子.
Not many str
methods will be as elegant as this because of the additional need to handle sentences with one word only.
您也可以在此处使用str.partition
.
u = df['Profile'].str.partition()
pd.DataFrame({'First': u[0], 'Last': u[2].str.split().str[-1]})
First Last
0 Technician NaN
1 Service Engineer
2 Sales Engineer
这篇关于从字符串中提取首尾两个单词作为pandas中的新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文