使用panda模块,将python中的array按列写入已有的excel
时间: 2023-04-03 08:04:10 浏览: 136
可以使用pandas模块中的DataFrame和to_excel方法来实现将Python中的array按列写入已有的Excel文件。具体操作可以参考以下代码:
```python
import pandas as pd
# 读取已有的Excel文件
df = pd.read_excel('example.xlsx')
# 将Python中的array转换为DataFrame
arr = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
df_new = pd.DataFrame(arr)
# 将DataFrame按列写入Excel文件
with pd.ExcelWriter('example.xlsx', mode='a') as writer:
df_new.to_excel(writer, sheet_name='Sheet1', startcol=df.shape[1], index=False, header=False)
```
其中,`mode='a'`表示以追加模式打开Excel文件,`startcol=df.shape[1]`表示从已有数据的下一列开始写入,`index=False`和`header=False`表示不写入行索引和列名。
相关问题
使用panda模块,将python中的array按列写入excel
可以使用 pandas 模块中的 DataFrame 和 to_excel 方法来实现将 Python 中的 array 按列写入 Excel。具体代码如下:
```python
import pandas as pd
# 创建一个 array
arr = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
# 将 array 转换为 DataFrame
df = pd.DataFrame(arr)
# 将 DataFrame 写入 Excel 文件
df.to_excel('output.xlsx', index=False, header=False)
```
其中,`index=False` 表示不将 DataFrame 的行索引写入 Excel,`header=False` 表示不将 DataFrame 的列名写入 Excel。如果需要写入行索引和列名,可以将这两个参数设置为 `True`。
阅读全文
相关推荐
















