Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find indices of nth occurance of a substring and subset the strings according to these indices in pandas

I have a dataframe with a columns that contains strings and i want to slice it from second space to 4th space. In general, in a column of strings, How do i 1. find the indices of nth occurance of a substring 2. subset the strings according to these indices

I am looking for a solution without involving regexes

like image 381
Nitin Siwach Avatar asked Dec 15 '25 05:12

Nitin Siwach


1 Answers

Try:

df['column'].str.split().str[2:4].str.join(' ')
like image 58
Chris Adams Avatar answered Dec 16 '25 18:12

Chris Adams



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!