如何从两个索引号之间的数据框中打印行

How to print rows from dataframe between two index numbers

#如何从两个索引号之间的数据框中打印行。

#下面的代码最后一行不工作。

index = df.index

condition1= df["main"] == "march"

condition2= df["main"] == "august"

row1 = index[condition1]
row2 = index[condition2]
i = row1.tolist()
j = row2. tolist()
df. iloc[i:j]

此问题是因为您将 list ij 传递给 iloc。您可以修改如下,应该可以解决您的问题。


index = df.index

condition1= df["main"] == "march"

condition2= df["main"] == "august"

row1 = index[condition1]
row2 = index[condition2]

i = row1.tolist()[0]
j = row2. tolist()[0]

df. iloc[i:j]