[英]How to export to multiple excel sheet of a single csv file through pandas in python
I have imported a large txt file in python pandas.我在 python pandas 中导入了一个大的 txt 文件。 Now I want to export the csv file to multiple excel as data is too large to fit in a single excel sheet.
现在我想将 csv 文件导出到多个 excel,因为数据太大而无法放入单个 Excel 工作表中。
I use the following commands:我使用以下命令:
import pandas as pd
df = pd.read_csv('basel.txt',delimiter='|')
df.to_excel('basel.txt')
Unfortunately I got the following error:不幸的是,我收到以下错误:
****ValueError: This sheet is too large! Your sheet size is: 1158008, 18 Max sheet size is: 1048576, 16384****
You can split into chunks and write each chunk in one sheet.您可以拆分成块并将每个块写在一张纸上。
np.array_split
splits into number of chunks np.split
requires an equal division. np.array_split
拆分为多个块np.split
需要等分。
import numpy as np
nsheets = 10 # you may change it
for i, temp in enumerate(np.array_split(df, nsheets)):
temp.to_excel('basel.xls', sheet_name=f'sheet_{i}')
You can write half of the dataset into a different excel sheet:您可以将数据集的一半写入不同的 Excel 表中:
import pandas as pd
df = pd.read_csv('basel.txt',delimiter='|')
df.iloc[:df.shape[0]//2,:].to_excel('basel.xls', sheet_name='First Sheet')
df.iloc[df.shape[0]//2:,:].to_excel('basel.xls', sheet_name='Second Sheet')
import pandas as pd
chunksize = 10 ** 6
for chunk in pd.read_csv('basel.txt', chunksize=chunksize):
chunk.to_excel('basel_'+str(chunk)+'.excel')
you may read the pandas file in chunks and save each chunk in excel file您可以分块读取pandas文件并将每个块保存在excel文件中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.