让Pandas计算出pd.read_excel中要跳过的行数

问题描述 投票:0回答:1

我正在尝试将数百个excel文件中的数据自动读入单个数据帧。值得庆幸的是,excel文件的布局相当稳定。它们都有相同的标题(标题的大小可能会有所不同),然后当然是相同数量的列,我想要读取的数据总是存储在第一个电子表格中。

但是,在某些文件中,在实际数据开始之前已跳过许多行。在实际数据之前的行中可能有也可能没有注释等。例如,在某些文件中,标题位于第3行,然后数据从第4行开始向下。

我希望pandas能够自己弄清楚要跳过多少行。目前我使用的是一个有点复杂的解决方案...我首先将文件读入数据帧,检查标头是否正确,如果没有搜索找到包含标题的行,然后重新读取文件,现在知道有多少行跳跃..

def find_header_row(df, my_header):
    """Find the row containing the header."""
    for idx, row in df.iterrows():
        row_header = [str(t).lower() for t in row]
        if len(set(my_header) - set(row_header)) == 0:
            return idx + 1
    raise Exception("Cant find header row!")

my_header = ['col_1', 'col_2',..., 'col_n']
df = pd.read_excel('my_file.xlsx')
# Make columns lower case (case may vary)
df.columns = [t.lower() for t in df.columns]

# Check if the header of the dataframe mathces my_header
if len(set(my_header) - set(df.columns)) != 0:
    # If no... use my function to find the row containing the header
    n_rows_to_skip = find_header_row(df, kolonner)
    # Re-read the dataframe, skipping the right number of rows
    df = pd.read_excel(fil, skiprows=n_rows_to_skip)

既然我知道标题行是什么样的,有没有办法让pandas在数据开始的地方弄清楚?或者有人能想到更好的解决方案吗?

python pandas
1个回答
1
投票

让我们知道这是否适合您

import pandas as pd
df = pd.read_excel("unamed1.xlsx")
df
    Unnamed: 0  Unnamed: 1  Unnamed: 2
0   NaN bad row1 badddd row111  NaN
1   baaaa   NaN NaN
2   NaN NaN NaN
3   id  name    age
4   1   Roger   17
5   2   Rosa    23
6   3   Rob 31
7   4   Ives    15
first_row = (df.count(axis = 1)  >= df.shape[1]).idxmax()
df.columns = df.loc[first_row]
df = df.loc[first_row+1:]
df
3   id  name    age
4   1   Roger   17
5   2   Rosa    23
6   3   Rob 31
7   4   Ives    15
© www.soinside.com 2019 - 2024. All rights reserved.