正文
pandas 将多个dataframe保存为一个excel文件的多个sheet表中
小程序:扫一扫查出行
【扫一扫了解最新限行尾号】
复制小程序
【扫一扫了解最新限行尾号】
复制小程序
# 创建文件
def create():
df1 = pd.DataFrame({"a1": [1, 2, 3], "b1": [4, 5, 6]})
df2 = pd.DataFrame({"a2": [1, 2, 3], "b2": [4, 5, 6]})
# 第一种方法 with
with pd.ExcelWriter(r"D:\test\test.xlsx") as xlsx:
df1.to_excel(xlsx, sheet_name="name1", index=False)
df2.to_excel(xlsx, sheet_name="name2", index=False)
print("保存完成")
# 第二种
xlsx = pd.ExcelWriter(r"D:\test\test.xlsx")
df1.to_excel(xlsx, sheet_name="name1", index=False)
df2.to_excel(xlsx, sheet_name="name2", index=False)
print("保存完成")
xlsx.close()
# 读取文件
xlsx = pd.ExcelFile(path)
df = pd.read_excel(xlsx, sheet_name="Sheet1")
# 读取文件第二种方式
df = pd.read_excel(path)
#保存在一个sheet表中
df.to_excel(path)