0

I am bringing in an excel sheet into a dataframe like below. I want to get all rows from Start and End.

start_row=df[df['Column1']=="Start"].index.values
end_row=df[df['Column1']=="End"].index.values

Is there a way to do something like df.iloc[[start_row[0]:end_row[0]]], I can do df.iloc[[start_row[0],end_row[0]]] but this only returns the Start Row and End Row and I need all in between.

Thank you!

Column1 Column2
Start    15
0        12
1         7
2         4
3         9
3        11
End      12 
1        18
2        24
etc      56

1 Answer 1

3

You can try

out = df.iloc[start_row[0]:end_row[0], :]
print(out)

  Column1  Column2
0   Start       15
1       0       12
2       1        7
3       2        4
4       3        9
5       3       11

Depending if want to include Start, you can optionally add 1 to start_row[0] like

out = df.iloc[start_row[0]+1:end_row[0], :]
Sign up to request clarification or add additional context in comments.

2 Comments

Works, thank you
@MarkProuty If your question is solved, say thank you by accepting the solution that is best for your needs. The accept check is below the up/down arrow at the top left of the answer. A new solution can be accepted if a better one shows up. If you have a reputation of 15 or greater, you may also vote on the quality of an answer, with the up or down arrow. Leave a comment if a solution doesn't answer the question. What should I do when someone answers my question?. Thank you.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.