Python 如何在执行startswith()方法后查找字符串的索引

Python 如何在执行startswith()方法后查找字符串的索引,python,pandas,Python,Pandas,我想搜索一列,直到找到strLinear,找到此位置的索引,向其中添加1,然后通过切片提取数据帧 我试过以下方法 for sheet_name, df in Input_Data.items(): for line in sheet_name: if line.startswith('Linear'): index = line.index('Linear') break df1 = df[index+1:236]

我想搜索一列,直到找到str
Linear
,找到此位置的索引,向其中添加1,然后通过切片提取数据帧

我试过以下方法

for sheet_name, df in Input_Data.items():
    for line in sheet_name:
        if line.startswith('Linear'):
            index = line.index('Linear')
            break
    df1 = df[index+1:236]
    df1.loc[:,'Unnamed: 26']*=-1  
    df1.loc[:,'Unnamed: 27']*=-1
    df=df1.sort_index(ascending=False)
    Indexer=df.columns.tolist()
    df = [(pd.concat([df[Indexer[0]],df[Indexer[num]]],axis=1)) for num in [1,2,3,4,5,6]]#concatenation
    df = [(df[num].astype(str).agg(','.join, axis=1)) for num in [0,1,2,3,4,5]]
    df=pd.DataFrame(df)
    df=df.loc[0].append(df.loc[1].append(df.loc[2].append(df.loc[3].append(df.loc[4].append(df.loc[5])))))

但是,我的“index”返回0,实际上它应该是125….

第一次检查时,您使用
startswith
,这意味着字符串以参数开始-索引0和forward,因此在这种情况下
index
将始终为0

我认为您要查找的循环是一个范围,或者索引号,以查找以该字符串开头的行,并引用该行之后的所有行

for index in range(len(sheet_name)):
    if sheet_name[index].startswith('Linear'):
        df1 = df[index+1:236]
        break
如果
line.startswith('Linear')
,则
line
中的第一个单词将是“Linear”

如果你想要的是行号,你可以试着找到它
enumerate
是你的朋友

def find_index_of(行、谓词):
对于索引,枚举中的行(行):
如果谓词(行):
回报指数
索引=查找索引(sheetname,λx:x.startswith(“线性”))

正确,这个元素将是一个数字,我可以从slicingI开始。为了完整起见,我用完整的代码更新了我的原始帖子。我已经插入了您建议的代码,不知怎么的,它在列中包含了不需要的字符串。我认为问题在于您建议的范围内索引(len(sheet_name))的第一行
,因为len()只是计算字符串的长度。我想你的意思是柱子的长度?