[译]使用Pandas读取大型Excel文件

上周我参加了dataisbeautiful subreddit上的Dataviz Battle,我们不得不从TSA声明数据集创建可视化。我喜欢这种比赛,因为大多数时候你最终都会学习很多有用的东西。
这次数据非常干净,但它分散在几个PDF文件和Excel文件中。在从PDF中提取数据的过程中,我了解了一些工具和库,最后我使用了tabula-py,这是Java库tabula的Python包装器。至于Excel文件,我发现单行 - 简单pd.read_excel- 是不够的。
最大的Excel文件大约是7MB,包含一个大约100k行的工作表。我虽然Pandas可以一次性读取文件而没有任何问题(我的计算机上有10GB的RAM),但显然我错了。
解决方案是以块的形式读取文件。该pd.read_excel函数没有像pd.read_sql这样的游标,所以我不得不手动实现这个逻辑。这是我做的:

import os
import pandas as pd


HERE = os.path.abspath(os.path.dirname(__file__))
DATA_DIR = os.path.abspath(os.path.join(HERE, '..', 'data'))


def make_df_from_excel(file_name, nrows):
    """Read from an Excel file in chunks and make a single DataFrame.

    Parameters
    ----------
    file_name : str
    nrows : int
        Number of rows to read at a time. These Excel files are too big,
        so we can't read all rows in one go.
    """
    file_path = os.path.abspath(os.path.join(DATA_DIR, file_name))
    xl = pd.ExcelFile(file_path)

    # In this case, there was only a single Worksheet in the Workbook.
    sheetname = xl.sheet_names[0]

    # Read the header outside of the loop, so all chunk reads are
    # consistent across all loop iterations.
    df_header = pd.read_excel(file_path, sheetname=sheetname, nrows=1)
    print(f"Excel file: {file_name} (worksheet: {sheetname})")

    chunks = []
    i_chunk = 0
    # The first row is the header. We have already read it, so we skip it.
    skiprows = 1
    while True:
        df_chunk = pd.read_excel(
            file_path, sheetname=sheetname,
            nrows=nrows, skiprows=skiprows, header=None)
        skiprows += nrows
        # When there is no data, we know we can break out of the loop.
        if not df_chunk.shape[0]:
            break
        else:
            print(f"  - chunk {i_chunk} ({df_chunk.shape[0]} rows)")
            chunks.append(df_chunk)
        i_chunk += 1

    df_chunks = pd.concat(chunks)
    # Rename the columns to concatenate the chunks with the header.
    columns = {i: col for i, col in enumerate(df_header.columns.tolist())}
    df_chunks.rename(columns=columns, inplace=True)
    df = pd.concat([df_header, df_chunks])
    return df


if __name__ == '__main__':
    df = make_df_from_excel('claims-2002-2006_0.xls', nrows=10000)

要记住的另一件事。当工作在Python Excel文件,你可能需要您是否需要从/读/写数据时使用不同的包.xls和.xlsx文件。
这个数据集包含两个.xls和.xlsx文件,所以我不得不使用xlrd来读取它们。请注意,如果您唯一关心的是读取.xlsx文件,那么即使xlrd 仍然可以更快,openpyxl也是可行的方法。
这次我没有写任何Excel文件,但如果你需要,那么你想要xlsxwriter。我记得用它来创建包含许多复杂工作表和单元格注释的工作簿(即Excel文件)。您甚至可以使用它来创建带有迷你图和VBA宏的工作表!

原文来源:https://www.giacomodebidda.com/reading-large-excel-files-with-pandas/

转载于:https://www.cnblogs.com/everfight/p/pandas_read_large_number.html

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值