Andrew
Andrew

Reputation: 537

Pandas substring using another column as the index

I'm trying to use one column containing the start index to subselect a string column.

df = pd.DataFrame({'string': ['abcdef', 'bcdefg'], 'start_index': [3, 5]})
expected = pd.Series(['def', 'g'])

I know that you can substring with the following

df['string'].str[3:]

However, in my case, the start index may vary, so I tried:

df['string'].str[df['start_index']:]

But it return NaNs.

EDIT: What if I don't want to use a loop / list comprehension; i.e. vectorized method preferred.

EDIT2: In this small test case, it seems like list comprehension is faster.

from itertools import islice
%timeit df.apply(lambda x: ''.join(islice(x.string, x.start_index, None)), 1)
%timeit pd.Series([x[y:] for x , y in zip(df.string,df.start_index) ])

631 µs ± 1.96 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
101 µs ± 233 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

Upvotes: 4

Views: 1995

Answers (1)

BENY
BENY

Reputation: 323226

Using for loop with zip of two columns , why we are using for loop here, you can check the link

[x[y:] for x , y in zip(df.string,df.start_index) ]
Out[328]: ['def', 'g']

Upvotes: 1

Related Questions