Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

selecting all cells between two string in a column

I posted question previously as "using “.between” for string values not working in python" and I was not clear enough, but I could not edit, so I am reposting with clarity here.

I have a Data Frame. In [0,61] I have string. In [0,69] I have a string. I want to slice all the data in cells [0,62:68] between these two and merge them, and paste the result into [1,61]. Subsequently, [0,62:68] will be blank, but that is not important.

However, I have several hundred documents, and I want to write a script that executes on all of them. The strings in [0,61] and [0,69] are always present in all the documents, but along different locations in that column. So I tried using:

For_Paste = df[0][df[0].between('DESCRIPTION OF WORK / STATEMENT OF WORK', 'ADDITIONAL REQUIREMENTS / SUPPORTING DOCUMENTATION', inclusive = False)]

But the output I get is: Series([], Name: 0, dtype: object)

I was expecting a list or array with the desired data that I could merge and paste. Thanks.

enter image description here

like image 392
spacedustpi Avatar asked May 10 '26 19:05

spacedustpi


1 Answers

If you want to select the rows between two indices (say idx_start and idx_end), excluding these two rows) on column col of the dataframe df, you will want to use

df.loc[idx_start + 1 : idx_end, col]

To find the first index matching a string s, use

idx = df.index[df[col] == s][0]

So for your case, to return a Series of the rows between these two indices, try the following:

start_string = 'DESCRIPTION OF WORK / STATEMENT OF WORK'
end_string = 'ADDITIONAL REQUIREMENTS / SUPPORTING DOCUMENTATION'
idx_start = df.index[df[0] == start_string][0]
idx_end = df.index[df[0] == end_string][0]
For_Paste = df.loc[idx_start + 1 : idx_end, 0]
like image 121
Fabian Ying Avatar answered May 14 '26 18:05

Fabian Ying