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文件的更多相关文章

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

    上周我参加了dataisbeautiful subreddit上的Dataviz Battle,我们不得不从TSA声明数据集创建可视化.我喜欢这种比赛,因为大多数时候你最终都会学习很多有用的东西. 这 ...

  2. 用pandas库修改excel文件里的内容,并把excel文件格式存为csv格式,再将csv格式改为html格式

    假设有Excel文件data.xlsx,其中内容为: ID  age  height     sex  weight张三   1   39     181  female      85李四   2  ...

  3. 用Python的pandas框架操作Excel文件中的数据教程

    用Python的pandas框架操作Excel文件中的数据教程 本文的目的,是向您展示如何使用pandas 来执行一些常见的Excel任务.有些例子比较琐碎,但我觉得展示这些简单的东西与那些你可以在其 ...

  4. POI读取/写入Excel文件

    import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io ...

  5. 根据NPOI 读取一个excel 文件的多个Sheet

    大家都知道NPOI组件可以再你本地没有安装office的情况下来 读取,创建excel文件.但是大家一般都是只默认读取一个excel文件的第一个sheet.那么如果要读取一个excel 的所有shee ...

  6. 建议42:使用pandas处理大型CSV文件

    # -*- coding:utf-8 -*- ''' CSV 常用API 1)reader(csvfile[, dialect='excel'][, fmtparam]),主要用于CSV 文件的读取, ...

  7. C# 读取大型Xml文件

    这篇博客将介绍在C#中如何读取数据量很大的Xml文件.请看下面的Xml文件, <?xml version="1.0" encoding="utf-8"?& ...

  8. 读取Excel二进制写入DB,并从DB中读取生成Excel文件

    namespace SendMailSMSService { class Program { static void Main(string[] args) { var connString = Sq ...

  9. Java入门开发POI读取导入Excel文件

    Apache POI是Apache开发的开源的跨平台的 Java API,提供API给Java程序对Microsoft Office格式档案进行各种操作. POI中Excel操作很简单,主要类有 HS ...

随机推荐

  1. python 装饰器 第三步:使用语法糖

    # 第三步:使用语法糖(就是语法) # 用于扩展基本函数的函数 def kuozhan(func): # 扩展功能1 print('饭前要洗手') # 调用基本函数 func() # 扩展功能2 pr ...

  2. bootstrap知识点

    首先,声明本次笔记是来自biaoyansu.com表严肃老师的bootstrap课程视频. 1.基本知识:1-1.首先,Html(理解:骨骼).Css(理解:皮肤).Js(理解:神经)分工不同.1-2 ...

  3. python外星人入侵(游戏开发)

    实现的项目要求: 1.外星人游戏添加飞船上下移动功能: 2.为游戏添加背景音乐: 3.在玩家得分.最高得分.玩家等级前添加"Score"."High Score" ...

  4. Jquery查找界面Html元素的方法(持续更新)

    1. 根据id来获取:$("#id") 2.根据class来获取:$(".class") 3.根据name来获取:$("[name=??]" ...

  5. js非布尔值的与(&&)与或(||)运算

    /** * 非布尔值的与(&&)与或(||)运算 * 1.先将其转换成布尔值再做运算,并且返回原值 * 2.与(&&)运算: * a.如果第一个值为true,则返回第二 ...

  6. 【记录】Mybatis-Generator 数据层代码生成器,自动生成dao类,mapper,pojo类

    Mybatis-Generator 工具来帮我们自动创建pojo类.mapper文件以及dao类并且会帮我们配置好它们的依赖关系. 官方文档地址:http://mybatis.org/generato ...

  7. vue-router中的router-link的active-class

    vue-router中的router-link的active-class   在vue-router中要使用选中样式的方法有两种: 1.直接在路由js文件中配置linkActiveClass 2.在r ...

  8. error: Error trying to parse settings: Unexpected trailing characters in Packages\User\Preferences.sublime-settings:9:2 reloading settings Packages/User/Preferences.sublime-settings

    error: Error trying to parse settings: Unexpected trailing characters in Packages\User\Preferences.s ...

  9. css强制换行和超出部分隐藏实现

    一.强制换行 1 word-break: break-all; 只对英文起作用,以字母作为换行依据. 2 word-wrap: break-word; 只对英文起作用,以单词作为换行依据. 3 whi ...

  10. 重磅 | 阿里云与MongoDB达成战略合作,成为全球唯一提供最新版MongoDB的云厂商

    MongoDB是业界最受欢迎的开源数据库之一,2019年一份面向开发者的数据库调查报告中,MongoDB以 24.6%的使用率占据次席. 阿里云是国内最早提供MongoDB服务的云厂商,提供完全兼容M ...