pandas -分割大型Excel文件 [英] Pandas - split large excel file
本文介绍了 pandas -分割大型Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个约有500,000行的excel文件,我想将其拆分为几个excel文件,每个文件有50,000行.
I have an excel file with about 500,000 rows and I want to split it to several excel file, each with 50,000 rows.
我想用熊猫来做,所以它将是最快和最简单的.
I want to do it with pandas so it will be the quickest and easiest.
任何想法如何实现?
感谢您的帮助
推荐答案
假定您的Excel文件只有一个(第一个)包含数据的表,我将使用chunksize
参数:
Assuming that your Excel file has only one (first) sheet containing data, I'd make use of chunksize
parameter:
import pandas as pd
import numpy as np
i=0
for df in pd.read_excel(file_name, chunksize=50000):
df.to_excel('/path/to/file_{:02d}.xlsx'.format(i), index=False)
i += 1
更新:
chunksize = 50000
df = pd.read_excel(file_name)
for chunk in np.split(df, len(df) // chunksize):
chunk.to_excel('/path/to/file_{:02d}.xlsx'.format(i), index=False)
这篇关于 pandas -分割大型Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文