简体   繁体   English

String.split 停止在最后一个元素 python

[英]String.split to stop at last element python

I am using str.split to split the data I have after a regex from one column into two columns.我正在使用 str.split 将正则表达式后的数据从一列拆分为两列。

The data is:数据是:

DATA_SET 
1  98
2 176
3 277 
.... 
16 790

DATA_SET
1   8
2  37
3 378
.... 
16 899

I have a piece of code that works with individual data sets to split the column into two columns.我有一段代码可用于单个数据集,将列拆分为两列。

f = pd.read_table('my_file.txt')

f[['D1 ', 'D2']] = f['DATA_SET'].str.split(expand=True)

How can I specify to the code that when it reaches the item “16” it needs to stop the split operation?如何向代码指定当它到达项目“16”时需要停止拆分操作? And do it again after it encounters the next “DATA_SET”并在遇到下一个“DATA_SET”后再次执行

Apply the split to the rows you are interested, ie where index is less equal 16将拆分应用于您感兴趣的行,即索引小于等于 16 的行

f[['D1 ', 'D2']] = f.loc[:16, 'DATA_SET'].\
    str.split(expand=True)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM