有没有办法读取所有行,直到使用Python Pandas遇到空行

我在Excel中有很多行,并且在空行之后行被填充了垃圾值。 有没有办法只使用Pythonpandas在Excel中的第一个空行之前的logging。

我不知道如果read_excel可以做到这一点。 如果你从excel中导入一个空行,这些行的列值将用NaN填充,那么你可以select这些值,直到第一行被所有的NaN填充。

我假设你的数据是这样的,你有一个空的行和数据后面是垃圾(我包括多个空行和垃圾跟着它) 在这里输入图像说明

df = pd.read_excel(r'Book1.xlsx') # read the file print df ''' col1 col2 col3 0 1 2 3 1 1 2 3 2 1 2 3 3 1 2 3 .... 10 1 2 3 11 NaN NaN NaN 12 xxx .... 18 NaN NaN NaN 19 NaN NaN NaN 20 yyy 21 yyy .... ''' first_row_with_all_NaN = df[df.isnull().all(axis=1) == True].index.tolist()[0] # gives me the first row number of the row that has all the values to be NaN. ''' 11 ''' print df.loc[0:first_row_with_all_NaN-1] # then I use loc to select the rows from 0 to first row with all NaN's-1 ''' col1 col2 col3 0 1 2 3 1 1 2 3 2 1 2 3 3 1 2 3 4 1 2 3 5 1 2 3 6 1 2 3 7 1 2 3 8 1 2 3 9 1 2 3 10 1 2 3 '''