大多数时候我只是在 pandas 中对它进行后处理,即在 pandas 中诊断、删除行并更正 dtypes。这样做的好处是更容易但可以说不那么优雅(我怀疑这样做也会更快!):
In [11]: df = pd.DataFrame([['blah', 1, 2], ['some_string', 3, 4], ['foo', 5, 6]])
In [12]: df
Out[12]:
0 1 2
0 blah 1 2
1 some_string 3 4
2 foo 5 6
In [13]: df[0].isin(['some_string']).argmax() # assuming it's found
Out[13]: 1
我实际上可能会在 python 中编写此代码,因为它可能对矢量化几乎/没有好处(而且我发现这更具可读性):
def to_skip(df, preceding):
for s in enumerate(df[0]):
if s in preceding:
return i
raise ValueError("No preceding string found in first column")
In [21]: preceding = ['some_string']
In [22]: to_skip(df, preceding)
Out[22]: 1
In [23]: df.iloc[1:] # or whatever you need to do
Out[23]:
0 1 2
1 some_string 3 4
2 foo 5 6
另一种可能性,就是搞乱 ExcelFile 并找到行号(同样使用上面的 for 循环,但在 openpyxl 或类似中)。但是,我不认为如果你这样做,会有一种方法可以只读取一次 excel 文件 (xml)。
与在 csv 上执行此操作相比,这有点不幸,您可以在 csv 中读取前几行(直到您看到所需的行/条目),然后传递此 opened 归档到read_csv。 (如果您可以将 Excel 电子表格导出为 csv,然后在 pandas 中进行解析,那会更快/更干净......)
注意:read_excel 并没有那么快(尤其是与read_csv 相比)...所以 IMO 你想尽快使用 pandas。