python - 使用 Python 和 Pandas 将 Excel 工作表拆分为单独的工作表

标签 python pandas dataframe

我需要一个脚本来将主工作表(包含超过 50K 行)拆分为仅包含 40 行且没有标题的单独工作表

经过一番研究,我成功创建了一个拆分主工作表的脚本。但是,每个工作表都包含原始标题,并且每个工作表的行不会拆分为 40 行。

我相信当您使用带有数据框的 panda 拆分工作表时,它们将始终包含标题?关于如何修改我的 python 脚本以实现我所需要的任何建议,或者是否有更简单的方法来实现这一目标而不需要使用 pandas 和数据框?

这是一个链接:https://github.com/lblake/sample-data一些示例数据

path = input('Enter file path to workbook name and extension, 
e.g. example.xlsx: ')
chunksize = int (input('Enter the row number you want to split the excel sheet at: ') )
destination = input('Enter folder path to where you want the split files stored. Press Enter to save in current location: ')

i = 0
df = pd.read_excel(path)
for chunk in np.array_split(df, len(df) // chunksize):
    chunk.to_excel(destination + 
'file_{:02d}.xlsx'.format(i), index=True)
i += 1 

最佳答案

我刚刚复制了您的代码并添加了 header=False

path = input('Enter file path to workbook name and extension, 
e.g. example.xlsx: ')
chunksize = int (input('Enter the row number you want to split the excel sheet at: ') )
destination = input('Enter folder path to where you want the split files stored. Press Enter to save in current location: ')

i = 0
df = pd.read_excel(path)
for chunk in np.array_split(df, len(df) // chunksize):
    chunk.to_excel(destination + 
'file_{:02d}.xlsx'.format(i), index=True, header=False)
i += 1 

这对我有用。

关于python - 使用 Python 和 Pandas 将 Excel 工作表拆分为单独的工作表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54023779/

相关文章:

python - Pandas 将列拆分为多级

python - 将压缩的 Stata 文件从 URL 读取到 pandas 中

python - Django admin list_display 属性使用

python - 如何知道Python列表中十进制值的索引

python - 从 numpy 制作数据框 - 为什么列是混合的?

python - Pandas:将自定义函数应用于组并将结果存储在每个组的新列中

python - 校准不失真以减小图像大小

python - 将当前 Python 程序置于后台

python - 如何将值列表写入 csv 文件中的 "cell"?

python - 对数据帧进行切片并根据不同切片添加值