如何使用pandas.read_excel基于正则表达式跳过行?
问题描述:
我想读一个excel表与pandas.read_excel。它的skiprows参数允许通过提供行号来跳过行。但是,我们如何根据模式匹配跳过行?我有不同的Excel表,其中我需要跳过的行数是可变的,所以提供行数对我的用例不起作用。有没有一种方法可以提供图案 - 例如在包含特定字符串的行之前跳过所有行(比如'Test')?如果这不能用pandas read_excel完成,那么有没有其他解决方法可以用这种方法将excel读入数据框?任何建议将不胜感激。谢谢。如何使用pandas.read_excel基于正则表达式跳过行?
答
我的建议是将整个Excel表格读入一个数据框,然后删除不需要的行。举个简单的例子:
import pandas as pd
# Read out first sheet of excel workbook
df = pd.read_excel('workbook.xlsx')
# Find label of the first row where the value 'Test' is found (within column 0)
row_label = (df.iloc[:, 0] == 'Test').idxmax()
# Drop all rows above the row with 'Test'
df = df.loc[row_label:, :]
+0
太好了,谢谢! –
你可以创建一个使用这些指标作为参数的'skip_rows'但没有样本数据和图形,我们可以”的引用格式的索引,然后遍历这个列表t提供更“具体”的答案 –