[英]Split string (object) into two columns on a character delimiter but KEEP character delimiter
In pandas, I am trying to split a column into two columns on a character and want to keep the character delimiter.在 Pandas 中,我试图将一个字符上的一列拆分为两列,并希望保留字符分隔符。
I have tried str.split() and it splits the columns the way I want but it deletes the character delimiter (as it is supposed to).我试过 str.split() 并且它按照我想要的方式拆分列,但它删除了字符分隔符(正如它应该的那样)。
My column looks like this.我的专栏看起来像这样。
mor
0 HR Ncfsa
I am using this code:我正在使用此代码:
df[['mor1', 'mor2']] = df['mor'].str.split(pat='^[A-Z]', n=-1, expand=True)
I get我得到
mor1 mor2
0 R Ncfsa
But I want to keep the first letter of the original column to get:但我想保留原始列的第一个字母以获得:
mor1 mor2
0 H R Ncfsa
IIUC, use Series.str.extract
IIUC,使用
Series.str.extract
df['mor'].str.extract('^(?P<mor1>[A-Z])(?P<mor2>.*)$')
[out] [出去]
mor1 mor2
0 H R Ncfsa
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.