简体   繁体   English

Pandas read_excel 只获取最后一行

[英]Pandas read_excel get only last row

I have an excel that is generated daily and can have up to 50k+ rows.我有一个每天生成的 excel,最多可以有 50k+ 行。 Is there a way to read only the last row (which is the sum of the columns)?有没有办法只读取最后一行(这是列的总和)?

right now I am just reading the entire sheet and keeping only the last row but it is taking up a huge amount of runtime.现在我只是在阅读整张纸并只保留最后一行,但它占用了大量的运行时间。

my code:我的代码:

df=pd.read_excel(filepath,header=1,usecols="O:AC")
df=df.tail(1)

Pandas is quite slow, especially with large in memory data. Pandas 非常慢,尤其是在 memory 数据量很大的情况下。 You can think about a lazy loading method, for example check dask .您可以考虑一种延迟加载方法,例如 check dask Else you can read the file using "open" and read the last line:否则,您可以使用“打开”读取文件并读取最后一行:

with open(filepath, "r") as file:
last_line = file.readlines()[-1]

I dont think there is a way to decrease runtime when you read excel file.当您阅读 excel 文件时,我认为没有办法减少运行时间。 When you read a excel or one sheet of excel,you would load excel all data into dask,even you use pd.read_excel skiprows,Its just keep the row the skiprows choose after you load all data into dask .So it cant decrease runtime. When you read a excel or one sheet of excel,you would load excel all data into dask,even you use pd.read_excel skiprows,Its just keep the row the skiprows choose after you load all data into dask .So it cant decrease runtime. If you really want decrease runtime of read file,you should save the file into another format,.csv or.txt and so on.如果您真的想减少读取文件的运行时间,您应该将文件保存为另一种格式,.csv 或.txt 等。

AND you generally you can't read Microsoft Excel files as a text files using methods like readlines or read.而且您通常无法使用 readlines 或 read 等方法将 Microsoft Excel 文件作为文本文件读取。 You should convert files to another format before (good solution is.csv which can be readed by csv module) or use a special python modules like pyexcel and openpyxl to read.xlsx files directly.您应该先将文件转换为另一种格式(好的解决方案是.csv模块可以读取的csv)或使用特殊的python模块直接读取文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM