Python 代码:合并两个 Excel 文件并生成新的表格
以下是一段 Python 代码,可以将一个 Excel 文件内容合并到另一个 Excel 文件内容下方,生成新的 Excel 表格:
import pandas as pd
# 读取两个 Excel 文件
file1 = pd.read_excel('file1.xlsx')
file2 = pd.read_excel('file2.xlsx')
# 将两个文件合并
merged_file = pd.concat([file1, file2], ignore_index=True)
# 生成新的 Excel 文件
merged_file.to_excel('merged_file.xlsx', index=False)
在上面的代码中,我们使用了 Pandas 库中的 read_excel() 函数来读取两个 Excel 文件。然后,我们使用 concat() 函数将两个文件合并成一个 DataFrame。最后,我们将合并后的 DataFrame 使用 to_excel() 函数生成新的 Excel 文件。
请注意,我们使用了 ignore_index=True 参数来忽略合并后的 DataFrame 中的原始索引,因为我们想要在新的 Excel 文件中重新生成索引。另外,我们使用了 index=False 参数来避免在生成的 Excel 文件中包含索引列。
原文地址: https://www.cveoy.top/t/topic/nJMs 著作权归作者所有。请勿转载和采集!