把好几个不同的panda的dataframe对象,分在不同时间段,保存为同一个xlsx里不同的sheet
时间: 2023-12-03 11:47:14 浏览: 88
可以使用 `pandas` 库的 `ExcelWriter` 类来实现将多个 `DataFrame` 对象保存到同一个 Excel 文件的不同 sheet 中。
以下是一个示例代码,假设有三个 pandas 的 DataFrame 对象 `df1`、`df2`、`df3`,需要将它们分别保存到名为 `Sheet1`、`Sheet2`、`Sheet3` 的不同 sheet 中:
```python
import pandas as pd
# 创建三个 DataFrame 对象 df1、df2、df3
# 创建 ExcelWriter 对象
writer = pd.ExcelWriter('output.xlsx')
# 将每个 DataFrame 写入不同 sheet 中
df1.to_excel(writer, sheet_name='Sheet1')
df2.to_excel(writer, sheet_name='Sheet2')
df3.to_excel(writer, sheet_name='Sheet3')
# 保存 Excel 文件
writer.save()
```
在上述代码中,通过 `pd.ExcelWriter()` 创建了一个 ExcelWriter 对象,并指定要保存的文件名为 `output.xlsx`。然后通过 `df.to_excel()` 方法将每个 DataFrame 写入到不同的 sheet 中,最后调用 `writer.save()` 方法保存文件并关闭 ExcelWriter 对象。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)