Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 让Pandas计算在pd.read\u excel中要跳过多少行_Python_Pandas - Fatal编程技术网

Python 让Pandas计算在pd.read\u excel中要跳过多少行

Python 让Pandas计算在pd.read\u excel中要跳过多少行,python,pandas,Python,Pandas,我正在尝试将数百个excel文件自动读取到单个数据框中。谢天谢地,excel文件的布局相当稳定。它们都有相同的标题(标题的大小写可能不同),当然还有相同的列数,我想读取的数据总是存储在第一个电子表格中 但是,在某些文件中,在实际数据开始之前跳过了许多行。在实际数据之前的行中可能有注释,也可能没有注释。例如,在某些文件中,标题位于第3行,然后数据从第4行开始,然后向下 我想pandas自己算出要跳过多少行。目前,我使用了一种有点复杂的解决方案…我首先将文件读入数据帧,检查标题是否正确,如果没有搜索

我正在尝试将数百个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
自己找出数据的起始位置?或者有人能想出更好的解决方案吗?

让我们知道这是否适合您

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

让我们知道这是否适合你

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

熊猫自动地关心它,如果你正在使用Read Tysv函数读取CSV文件,那么它忽略了标题栏上方的空白行,你尝试过<代码> SkiPRWS PARAM吗?请注意,它可能是一个
可调用的
,如果跳过该行,则返回True,如果返回False,则返回Falseotherwise@min2bro我读的不是.csv,而是.xlsx。它自己没有做正确的事情。@Neroksi我没有遵循:|你能提供一个代码示例吗?@mortysporty抱歉,我的解决方案不起作用,因为pandas只会传递行的索引,而不会传递整行的数据,如果行无效,可以在返回
True
之前检查这些数据。pandas会自动处理它,如果您正在使用Read Tysv函数读取CSV文件,那么它忽略了标题栏上方的空白行,您尝试过<代码> SKIPROWS PARAM吗?请注意,它可能是一个
可调用的
,如果跳过该行,则返回True,如果返回False,则返回Falseotherwise@min2bro我读的不是.csv,而是.xlsx。它自己没有做正确的事情。@Neroksi我没有遵循:|你能提供一个代码示例吗?@mortysporty抱歉,我的解决方案不起作用,因为pandas只传递行的索引,而不传递整行的数据,如果行无效,可以在返回
True
之前检查这些数据。