Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: How to get rows between indexes?

Good day everyone. My problem is: I can't get rows (data) between several rows. I've got One excel book with 21 sheets. Every table on every sheet with some different changes. Tables haven't heads (people who made it - are idiots). But there are same values in first and second columns in tables. So, i get their indexes, but how to get rows between them? enter image description here

import pandas as pd

file_path = r"./files/menu.xlsx"

df = pd.read_excel(file_path)
xl = pd.ExcelFile(file_path).sheet_names
breakfast = df.loc[df['Unnamed: 0'] == 'breakfast'].index[0]   # index is "5"
dinner = df.loc[df['Unnamed: 0'] == 'dinner'].index[0]         # index is "14"
heads = df.iloc[[int(breakfast) - 1]]
like image 809
alek vertysh Avatar asked Oct 16 '25 18:10

alek vertysh


1 Answers

You can use the range start:end in the iloc() operation to specify start and end indexes. Remember that the start index is included in the result while the end index is excluded:

rows_between = df.iloc[int(breakfast)+1:int(dinner)]
like image 73
Davide Anghileri Avatar answered Oct 18 '25 07:10

Davide Anghileri



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!