pandas按列写入excel
时间: 2023-04-04 20:02:46 浏览: 63
可以使用 pandas 库中的 to_excel() 方法将 DataFrame 按列写入 Excel 文件中。具体操作可以参考以下代码:
```python
import pandas as pd
# 创建 DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})
# 将 DataFrame 按列写入 Excel 文件
df.to_excel('output.xlsx', index=False)
```
这样就可以将 DataFrame 内容按列写入名为 "output.xlsx" 的 Excel 文件中了。
相关问题
pandas按列追加写入excel
可以使用 pandas 库中的 to_excel 方法,将数据按列追加写入 Excel 文件中。具体操作可以参考以下代码:
```python
import pandas as pd
# 读取已有的 Excel 文件
df = pd.read_excel('example.xlsx')
# 新增数据
new_data = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
# 将新数据按列追加写入 Excel 文件
with pd.ExcelWriter('example.xlsx', mode='a') as writer:
new_data.to_excel(writer, sheet_name='Sheet1', index=False, header=False, startcol=df.shape[1])
```
其中,mode='a' 表示以追加模式打开 Excel 文件,sheet_name='Sheet1' 表示写入到名为 Sheet1 的工作表中,index=False 和 header=False 表示不写入行索引和列名,startcol=df.shape[1] 表示从已有数据的最后一列开始写入新数据。
pandas写入excel列
可以使用 pandas 库中的 to_excel() 方法将数据写入 Excel 文件的指定列中。例如,以下代码将数据写入名为 "Sheet1" 的 Excel 文件的第一列中:
```python
import pandas as pd
# 创建数据
data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35]}
# 将数据转换为 DataFrame
df = pd.DataFrame(data)
# 将 DataFrame 写入 Excel 文件的第一列中
df.to_excel('example.xlsx', sheet_name='Sheet1', startcol=0, index=False)
```
在上面的代码中,startcol=0 表示从第一列开始写入数据,index=False 表示不将 DataFrame 的索引写入 Excel 文件中。