2017-06-02 37 views
0

我想讀一個excel表與pandas.read_excel。它的skiprows參數允許通過提供行號來跳過行。但是,我們如何根據模式匹配跳過行?我有不同的Excel表,其中我需要跳過的行數是可變的,所以提供行數對我的用例不起作用。有沒有一種方法可以提供圖案 - 例如在包含特定字符串的行之前跳過所有行(比如'Test')?如果這不能用pandas read_excel完成,那麼有沒有其他解決方法可以用這種方法將excel讀入數據框?任何建議將不勝感激。謝謝。如何使用pandas.read_excel基於正則表達式跳過行?

+0

你可以創建一個使用這些指標作爲參數的'skip_rows'但沒有樣本數據和圖形,我們可以」的引用格式的索引,然後遍歷這個列表t提供更「具體」的答案 –

回答

0

我的建議是將整個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

太好了,謝謝! –