简体   繁体   English

如何在 excel 的一个单元格中插入 pandas 数据帧(使用 openpyxl),其中的值将用逗号分隔?

[英]How to insert a pandas data frame in one cell in excel (using openpyxl), where the values will be separated with comma?

I would like to insert a pandas data frame in one cell in excel (using openpyxl), where the values of the data frame will be separated with comma.我想在 excel 的一个单元格中插入一个 pandas 数据帧(使用 openpyxl),其中数据帧的值将用逗号分隔。 Until now I have achieved to print the data frame in excel but in multiple cells, as it is presented in the following script and table.到目前为止,我已经实现了在 excel 中打印数据帧,但在多个单元格中,如下面的脚本和表格中所示。 Moreover, in the last figure, the table that I want to print in excel is presented.此外,在最后一个图中,显示了我要在 excel 中打印的表格。

import pandas as pd
from openpyxl import load_workbook 
from openpyxl.utils.dataframe import dataframe_to_rows

wb = load_workbook('test.xlsx')
ws = wb['Sheet1'] 
df = pd.DataFrame({'Data': [1, 2, 3, 4, 5, 6]}) 
df_transpose=df.T
rows_materials= dataframe_to_rows(df.T, index=False,header=False)

for r_idx_materials, row in enumerate(rows_materials, 1):
    for c_idx_materials, value in enumerate(row, 1): 
         ws.cell(row=r_idx_materials, column=c_idx_materials, value=value)

wb.save('test.xlsx')

在此处输入图像描述

在此处输入图像描述

You can pass the contents of the list in as a string like this您可以像这样将列表的内容作为字符串传递


wb = load_workbook(r'C:\temp\temp.xlsx')
ws = wb['Sheet1'] 
my_list = ["1, 2, 3, 4, 5, 6"]
df = pd.DataFrame({'Data': my_list}) 
df_transpose=df.T
rows_materials= dataframe_to_rows(df.T, index=False,header=False)

for r_idx_materials, row in enumerate(rows_materials, 1):
    for c_idx_materials, value in enumerate(row, 1): 
         ws.cell(row=r_idx_materials, column=c_idx_materials, value=value)

wb.save(r'C:\temp\temp.xlsx')

Output Output 在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 python 分隔 pandas 数据帧中的嵌套逗号分隔列值? - How to separate nested comma separated column values in pandas data frame using python? OpenPyXl:我可以从一个 Excel 文件中引用数据并根据标识符将值插入到单元格中吗? - OpenPyXl: Can I reference data from one Excel file and insert values into a cell based on an identifier? 如何从 pandas 数据帧中的逗号分隔值计算以特定 substring 开头的字符串的出现次数? - How to count the occurrences of a string starts with a specific substring from comma separated values in a pandas data frame? 如何使用 openpyxl 在 XLSX 文件中逐个单元格地插入和格式化值 - How to insert and format values cell by cell in an XLSX file using openpyxl 如何使用 Python 在一个单元格中读取逗号分隔的字符串 - How to read comma separated string in one cell using Python 如何在熊猫数据框中将join()与groupby一起使用,以便可以使用分隔符分隔值 - How to use join() with groupby in pandas data frame so that the values can be separated using a separator 如何使用 pandas 数据框将数据框的每一列值添加到一张一张的新工作表中 - How to add each column of a data frame values in one by one new sheets using pandas data frame 使用openpyxl在excel工作表中打印python数据帧时如何跳过默认数据帧索引 - How to skip the default data frame index, when printing a python data frame in an excel worksheet, using openpyxl 如何将 pandas 数据帧内的嵌套逗号分隔列转换为 Python 中的特定格式 - How to convert nested comma separated column inside a pandas data frame to specific format in Python 如何使用openpyxl在excel图表中仅显示一个数据标签? - How to show only one data label in excel chart using openpyxl?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM