帮助改进我在 python 中的“read_excel”执行时间。我的代码读得很慢

数据挖掘 Python 擅长 时间
2022-02-18 11:42:16

我在这里的第一个问题,所以请告诉我。

我正在尝试用从 excel 文件中读取的训练数据来为我的神经网络提供数据。当我在工作表中的行数少于 50 时,它工作得非常好。但是当我尝试使用包含近 4.000 行的真正 excel 文件时,它突然需要永远。虽然 4.000 很多,但我很确定我的做法仍然非常低效。

正如您在下面的代码中看到的那样,我在循环中一遍又一遍地使用 read_excel。我觉得应该有一种方法可以只阅读整个专栏 1 次,然后从那里开始使用它。

我的目标是从第 0 行开始读取 5 行作为第一个输入。然后再次读取 5 行,但从第 1 行开始,再从第 3 行开始读取 5 行所以它就像一个 5 行的窗口,被读取然后移动窗口加 1。输出应始终是窗口后的 1 行。

**Example:** if row 1-20 contains numbers 1-20 then:
input1 = [1,2,3,4,5] and output1 = 6
input2 = [2,3,4,5,6] and output2 = 7
...
input15 = [15,16,17,18,19] and output15 = 20
注意输入是列表,输出只是数字。因此,当我将它们附加到最终的输入和输出列表时,我最终得到的输入是列表列表,而输出是输出列表

我的代码

from pandas import read_excel

# initialize final input & output lists. The contents of the temporary input & output lists
# are gonna be appended to these final lists
training_input = []
training_output = []

# excel relevant info
my_sheet = 'Junaid'
file_name = '../Documents/Junaid1.xlsx'

# initialize counters
loop_count = 0
row_counter = 0

for x in range(25):

    # load the excel file containing inputs & outputs
    # using parameters skiprows, nrows (number of rows) and index col
    df = read_excel(file_name, sheet_name = my_sheet, skiprows=row_counter, nrows=6, index_col=0)

    # initialize temporary input & output lists
    input_temp = []
    output_temp = []

    for y in df.index:
        # append the first 5 rows of the 6 to input list
        if loop_count < 5:
            input_temp.append(df.index[loop_count])
            loop_count += 1
        else:
            # append the 6th data to output list
            training_output.append(df.index[loop_count])

    training_input.append(input_temp)
    row_counter += 1
    loop_count = 0
1个回答

是的,这会很慢,因为您要为 for 循环的每次迭代打开和关闭文件。编程中的一般规则是,如果文件不是不断变化的,则只打开并读取一次。此外,如果您仅使用列表推导式,您的大部分代码都可以被删除

在这里,我重写了您的代码,只打开文件并读取一次,然后使用列表理解和切片创建两个列表。

from pandas import read_excel

# excel relevant info
my_sheet = 'Junaid'
file_name = '../Documents/Junaid1.xlsx'

df = read_excel(file_name, sheet_name = my_sheet, index_col=0, header=None)

training_input = [df.index[i:i+5].tolist() for i in range(len(df)-5)]
training_output = [df.index[i].tolist() for i in range(5, len(df))]

此外,您的代码中似乎存在错误,因为您在问题中描述的 excel 文件没有标题(即第一行包含数据),因此您的代码会跳过第一行值。要解决这个问题,您应该将参数“header=None”传递给 pandas 函数,告诉它没有标题索引。您可以在此处阅读更多相关信息。