Python 如何获取数据帧的索引范围

Python 如何获取数据帧的索引范围,python,pandas,Python,Pandas,获取相应列内容满足条件的索引范围的最有效方法是什么。。类似于以标记开始并以“body”标记结束的行 例如,数据框如下所示 我想得到行索引1-3 有人能提出最具蟒蛇风格的方法来实现这一点吗 import pandas as pd df=pd.DataFrame([['This is also a interesting topic',2],['<body> the valley of flowers ...',1],['found in the hilly terrain',5],

获取相应列内容满足条件的索引范围的最有效方法是什么。。类似于以标记开始并以“body”标记结束的行

例如,数据框如下所示

我想得到行索引1-3

有人能提出最具蟒蛇风格的方法来实现这一点吗

import pandas as pd

df=pd.DataFrame([['This is also a interesting topic',2],['<body> the valley of flowers ...',1],['found in the hilly terrain',5],
             ['we must preserve it </body>',6]],columns=['description','count'])

print(df.head())
将熊猫作为pd导入
df=pd.DataFrame([['这也是一个有趣的话题',2],'花卉谷',1],'发现于丘陵地带',5],
['we must preserve it',6]],columns=['description','count'])
打印(df.head())

您希望满足什么条件

import pandas as pd

df=pd.DataFrame([['This is also a interesting topic',2],['<body> the valley of flowers ...',1],['found in the hilly terrain',5],
             ['we must preserve it </body>',6]],columns=['description','count'])
print(df)
print(len(df[df['count'] != 2].index))

帮助信息:

您还可以找到起始行和结束行的索引,然后在它们之间添加行,以获取它们之间的所有内容

start_index = df[df['description'].str.contains("<body>")==True].index[0]
end_index = df[df['description'].str.contains("</body>")==True].index[0]

print(df["description"][start_index:end_index+1].sum())
start\u index=df[df['description'].str.contains(“”==True]。索引[0]
end_index=df[df['description'].str.contains(“”==True]。索引[0]
打印(df[“description”][开始索引:结束索引+1].sum())

请不要发布代码或数据的图像@第二次世界大战我会记住这一点。谢谢你提供的信息。抱歉,我忘了在问题中添加条件。。我现在看到的条件是,以,tag开头和结尾的行
start_index = df[df['description'].str.contains("<body>")==True].index[0]
end_index = df[df['description'].str.contains("</body>")==True].index[0]

print(df["description"][start_index:end_index+1].sum())