Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python和Pandas将Excel工作表拆分为单独的工作表_Python_Pandas_Dataframe - Fatal编程技术网

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

使用Python和Pandas将Excel工作表拆分为单独的工作表,python,pandas,dataframe,Python,Pandas,Dataframe,我需要一个脚本将主工作表(包含超过50K行)拆分为单独的工作表,这些工作表只包含40行,没有标题 经过一点研究后,我成功地创建了一个脚本来拆分主工作表。但是,每个工作表都包含原始标题,并且每个工作表的行不会拆分为40行 我相信,当您使用带有数据框的panda拆分工作表时,它们将始终包含标题?有没有关于如何修改python脚本以实现所需的内容的建议,或者有没有更简单的方法来实现这一点,而不需要使用熊猫和数据帧 这里有一个链接:指向一些示例数据 path = input('Enter file pa

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

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

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

这里有一个链接:指向一些示例数据

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 

您可以使用
groupby
并进行迭代。要忽略标题,请在写入
pd.ExcelWriter
对象时指定
header=False
。下面的示例将10行的数据帧拆分为2行块

df = pd.DataFrame(np.arange(100).reshape((10, 10)))

writer = pd.ExcelWriter('file.xlsx')

for key, grp in df.groupby(df.index // 2):
    grp.to_excel(writer, f'sheet_{key}', header=False)

writer.save()

我刚刚复制了您的代码并添加了
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 

这对我很有效。

谢谢在我的代码中添加header=False选项,修复了我的问题