python pandas 批量构建dataFrame 写入excel
时间: 2023-04-04 07:02:03 浏览: 84
可以使用 pandas 的 to_excel 方法将 DataFrame 写入 Excel 文件。具体操作可以参考以下代码:
import pandas as pd
# 构建数据
data = {'姓名': ['张三', '李四', '王五'], '年龄': [20, 25, 30], '性别': ['男', '女', '男']}
# 批量构建 DataFrame
df_list = []
for i in range(3):
df_list.append(pd.DataFrame(data))
# 写入 Excel 文件
with pd.ExcelWriter('output.xlsx') as writer:
for i, df in enumerate(df_list):
df.to_excel(writer, sheet_name=f'Sheet{i+1}', index=False)
相关问题
python pandas 按行构建dataFrame 写入excel
可以使用 pandas 库中的 DataFrame 函数来构建 dataFrame,然后使用 to_excel 方法将其写入 excel 文件中。具体的代码实现可以参考以下示例:
import pandas as pd
# 构建 dataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})
# 写入 excel 文件
df.to_excel('output.xlsx', index=False)
其中,DataFrame 函数中的参数可以根据实际情况进行调整,to_excel 方法中的参数 index=False 表示不将行索引写入 excel 文件中。
python把dataframe写入excel
你可以使用 pandas 库将 DataFrame 写入 Excel 文件。以下是一个示例代码:
```python
import pandas as pd
# 创建一个 DataFrame
data = {'name': ['John', 'Tom', 'Amy'], 'age': [25, 32, 18]}
df = pd.DataFrame(data)
# 将 DataFrame 写入 Excel 文件
with pd.ExcelWriter('output.xlsx') as writer:
df.to_excel(writer, sheet_name='Sheet1', index=False)
```
在这个示例中,我们首先创建了一个包含姓名和年龄的 DataFrame。然后,我们使用 `pd.ExcelWriter()` 函数创建了一个 ExcelWriter 对象,并指定输出文件名为 `output.xlsx`。接下来,我们使用 `to_excel()` 函数将 DataFrame 写入 Excel 文件。在此函数中,我们指定输出到“Sheet1”工作表,并将 index 参数设置为 False,以避免将 DataFrame 的索引写入 Excel 文件中。
请注意,在写入 Excel 文件之前,你需要安装 pandas 库。你可以使用以下命令在命令行中安装:
```
pip install pandas
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)