【问题标题】:How to search a particular string in the excel sheet and print the next row of the searched string如何在Excel工作表中搜索特定字符串并打印搜索到的字符串的下一行
【发布时间】:2023-02-08 19:12:16
【问题描述】:

我想在 Excel 工作表中搜索包含 1000 行的字符串

我用了

substring=" "
df[df.apply(lambda row:row.astype(str).str.contains(substring,case=False).any(),axis=1)]

这将返回我搜索过的字符串,但我还想打印搜索到的字符串旁边的行。我怎样才能做到这一点?

【问题讨论】:

  • 你的意思是数据框?
  • 您的意思是包含子字符串的任何行吗?至少为您想要的输出提供最少的数据。
  • 请阐明您的具体问题或提供更多详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。

标签: python


【解决方案1】:

使用循环尝试这种方法:

substring=" "
filtered_rows = df[df.apply(lambda row:row.astype(str).str.contains(substring,case=False).any(),axis=1)]

for i, current_row in enumerate(filtered_rows[:-1].itertuples()):
    next_row = filtered_rows.iloc[i+1]
    print(next_row)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-03
    • 1970-01-01
    • 2019-04-02
    • 2012-01-06
    • 2017-11-17
    • 2023-02-10
    相关资源
    最近更新 更多