简体   繁体   English

将具有 52000 行的 .xlsx 文件分成 11 个 excel 文件的 Python 代码(10 个具有 5000 行的 xlsx 文件和 1 个具有剩余 2000 条记录的文件)

[英]Python code to break .xlsx file having 52000 rows into 11 excel files (10 xlsx files with 5000 rows and 1 file with remaining 2000 records)

I am trying to write a Python code to break.xlsx file having 52000 rows into 11 excel files (10 xlsx files with 5000 rows and 1 file with remaining 2000 records)我正在尝试编写一个 Python 代码,将具有 52000 行的 break.xlsx 文件转换为 11 个 excel 文件(10 个具有 5000 行的 xlsx 文件和一个具有剩余 2000 条记录的文件)

having a hard time finding a good online solution很难找到一个好的在线解决方案

Tried the below solution which is producing exact desired outcome but it is also producing blank 49,996 xlsx files尝试了以下解决方案,它产生了准确的预期结果,但它也产生了空白的 49,996 xlsx 文件

import pandas as pd
df = pd.read_excel("C:\\Users\\rajat.kapoor\\Desktop\\All Data Combined\\Credit Check Data.xlsx")
n_partitions = 5000

for i in range(n_partitions):
    sub_df = df.iloc[(i*n_partitions): ((i+1)*n_partitions)]
    sub_df.to_excel(f"C:\\Users\\rajat.kapoor\\Desktop\\All Data Combined\\Credit Check Data - {i}.xlsx", sheet_name="a")

you can change the increment step from 1 to 5000. So you would only create 11 excel files.您可以将增量步长从 1 更改为 5000。因此您只会创建 11 个 excel 文件。 Since rest similar I am showing the different part below.由于休息相似,我在下面展示了不同的部分。

for i in range(0,52000,5000):

   sub_df = df.iloc[(i): ((i+5000))]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM