简体   繁体   中英

Using slice() but want to use start, and length. Is this possible?

I have almost a hundred columns to classify from a mainframe file with no delimits and am using slice functionality to achieve this. I'm slicing each location into a list, then combining these lists to then create a Pandas dataframe to perform additional analysis on.

    a = slice(0,10)
    b = slice(10,25)

However, I know the start location, and the length of each string. Is there a similar way to classify using start and length instead of start and end?

Python

>>> x = 'hello world'
>>> start = 6
>>> length = 5
>>> x[start:start+length]
'world'

Pandas

>>> import pandas as pd
>>> df = pd.DataFrame({'x':['hello world']})
>>> df['y'] = df['x'].str.slice(start, start+length)
>>> df
             x      y
0  hello world  world

Can't you just use start+length as the end argument?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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