[英]How to insert a pandas data frame in one cell in excel (using openpyxl), where the values will be separated with comma?
[英]How to skip the default data frame index, when printing a python data frame in an excel worksheet, using openpyxl
我正在嘗試使用 openpyxl 在 excel 表中打印 pandas 數據框。 到目前為止,我已經完成了使用默認索引打印數據框。 我的問題是,如何在沒有默認索引的情況下打印它。 以下示例腳本生成表 1 ,我想知道我需要修改什么才能得出表 2 。
import pandas as pd
from openpyxl.utils.dataframe import dataframe_to_rows
df = pd.DataFrame((1,2,3,4,5,6))
wb = openpyxl.Workbook()
sheet = wb.active
rows = dataframe_to_rows(df)
for r_idx, row in enumerate(rows, 1):
for c_idx, value in enumerate(row, 1):
sheet.cell(row=r_idx+1, column=c_idx, value=value)
wb.save('book1.xlsx')
表格1
0
0 1
1 2
2 3
3 4
4 5
5 6
表 2
1
2
3
4
5
6
除非您有更詳細的要求,否則 go 直接使用pandas.DataFrame.to_excel
更容易,它已經與openpyxl
集成
df.to_excel('book1.xlsx')
要刪除索引,只需添加:
df.to_excel('book1.xlsx', index=False)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.