使用Pandas读取大型Excel文件
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})")
print(f"文件名:{file_name}")
print(f"工作表:{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)")
print(f"行数:{df_chunk.shape[0]}")
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('/Users/mac/Desktop/Data/demo.xlsx', nrows=1000000)
from: cnblogs.com/everfight/p/pandas_read_large_number.html
使用Pandas读取大型Excel文件的更多相关文章
- [译]使用Pandas读取大型Excel文件
上周我参加了dataisbeautiful subreddit上的Dataviz Battle,我们不得不从TSA声明数据集创建可视化.我喜欢这种比赛,因为大多数时候你最终都会学习很多有用的东西. 这 ...
- 用pandas库修改excel文件里的内容,并把excel文件格式存为csv格式,再将csv格式改为html格式
假设有Excel文件data.xlsx,其中内容为: ID age height sex weight张三 1 39 181 female 85李四 2 ...
- 用Python的pandas框架操作Excel文件中的数据教程
用Python的pandas框架操作Excel文件中的数据教程 本文的目的,是向您展示如何使用pandas 来执行一些常见的Excel任务.有些例子比较琐碎,但我觉得展示这些简单的东西与那些你可以在其 ...
- POI读取/写入Excel文件
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io ...
- 根据NPOI 读取一个excel 文件的多个Sheet
大家都知道NPOI组件可以再你本地没有安装office的情况下来 读取,创建excel文件.但是大家一般都是只默认读取一个excel文件的第一个sheet.那么如果要读取一个excel 的所有shee ...
- 建议42:使用pandas处理大型CSV文件
# -*- coding:utf-8 -*- ''' CSV 常用API 1)reader(csvfile[, dialect='excel'][, fmtparam]),主要用于CSV 文件的读取, ...
- C# 读取大型Xml文件
这篇博客将介绍在C#中如何读取数据量很大的Xml文件.请看下面的Xml文件, <?xml version="1.0" encoding="utf-8"?& ...
- 读取Excel二进制写入DB,并从DB中读取生成Excel文件
namespace SendMailSMSService { class Program { static void Main(string[] args) { var connString = Sq ...
- Java入门开发POI读取导入Excel文件
Apache POI是Apache开发的开源的跨平台的 Java API,提供API给Java程序对Microsoft Office格式档案进行各种操作. POI中Excel操作很简单,主要类有 HS ...
随机推荐
- JNDI数据源的配置及使用
数据源的作用JDBC操作的步骤: 1. 加载驱动程序 2. 连接数据库 3. 操作数据库 4. 关闭数据库,释放连接 也就是说,所有的用户都需要经过此四步进行操作,但是这四步之中有三步对所有人 ...
- flink-training-course
目录 flink-training-course 大数据领域顶级盛会 Flink Forward Asia 2019 详情
- Python 学习笔记13 类 - 创建和简单使用
介绍: 面向对象编程是一种非常有效的软件编写方法之一,在面向对象编程中,我们会编写表示现实世界中的事物或者情景的类,并基于类来创建对象. 在编写类的的时候,这些类对象一般都有通用的行为或者属性.基于类 ...
- PHP字符串操作函数练习20191025
<?php$arr=["tom","peter","mary"];$str=implode("+",$arr);/ ...
- Android No static field XXX of type I in class Lcom/XXX/R$id错
问题复现: 问题原因: 出现这样的情况,你先检查你的依赖工程(module)的对应布局layout/xxx.xml是否跟主项目的layout重名,你点开R文件的时候,你会发现你的布局发生了错乱,导致你 ...
- ThinkPHP5.1x 中间件实现原理
ThinkPHP5.1x的中间件,其核心还是闭包函数的应用,来实现“责任链”模式: 模拟代码: <?php //模拟的控制器 class Controller { public function ...
- gym/102253C Colorful Tree 树上计数
题意:有一颗数,每个点有一个颜色,定义两点之间的距离为两点路径之间不同颜色的数目,问所有路径的距离和是多少? 思路:每个颜色的贡献为路径中有这个颜色的路径数.先假设所有路径都会经过一种颜色,再减去不会 ...
- 【C/C++】知识点系统复习 (第一周)
2018/12/18 周二 1. C++内存布局分为几个区域,每个区域有什么特点? 主要可以分为 5 个区域, (1) 栈区:由编译器自动分配释放,存放函数的参数值,局部变量的值等.其操作方式类似于数 ...
- Flask-sqlalchemy-表关系
表关系 表之间的关系存在三种: 一对一.一对多.多对多. 而SQLAlchemy中的ORM也可以模拟这三种关系.因为一对一其实在SQLAlchemy中底层是通过一对多的方式模拟的, ...
- ubuntu下oracle 数据库安装
环境:腾讯云 一. 由于腾讯云直接下载oracle太慢,先安装docker 1.sudo apt update 2.接下来,使用apt安装一些允许通过HTTPS才能使用的软件包: sudo apt i ...