简体   繁体   English

如何通过python中的pandas导出单个csv文件的多个excel表

[英]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.

相关问题 通过python pandas将3个SQL查询结果导出到excel表中的3个表中 - Export 3 SQL Query results to 3 sheets in an excel sheet through python pandas 如何从多个 csv 文件中读取数据并写入 Python 中的单个 Excel 表的同一张表 - How to read data from multiple csv files and write into same sheet of single Excel Sheet in Python Python Pandas - 循环浏览 Excel 文件的文件夹,将每个 ZC1D81AF583580.x 文件的数据导出到它们自己的文件“DED8FDCZ”中 - Python Pandas - loop through folder of Excel files, export data from each Excel file's sheet into their own .xlsx file Python 多张表导出为 csv - Python Multiple sheet to export as csv 如何将 dfs 导出到带有多个工作表和不同工作表名称的 excel 熊猫 - How to export dfs to excel with multiple sheets and different sheet names pandas 如何使用Pandas将python Web抓取数据导出到现有excel文件中的特定工作表? - How can I export my python web scrape data to a specific sheet in an existing excel file using pandas? 如何使用Pandas从python将多个数据透视表导出到单个csv文档? - How do I export multiple pivot tables from python using pandas to a single csv document? 如何通过Pandas Dataframe在单个csv文件中附加多个csv文件记录 - how to append multiple csv files records in a single csv file through Pandas Dataframe 如何在python中使用Pandas遍历Excel工作表的日期标头? - How to iterate through date header of excel sheet using pandas in python? 熊猫-将多个数据框写入单个Excel工作表 - Pandas - Write multiple dataframes to single excel sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM