【问题标题】:How to create sheets in an Excel file with pandas and python?如何使用 pandas 和 python 在 Excel 文件中创建工作表?
【发布时间】:2020-01-13 06:03:06
【问题描述】:

假设我有以下 Excel 文件: enter image description here

我想使用 Python 为每个工作表创建一个 Excel 文件,以及所有列。因此,在 Sheet1 中包含 A 列中提到“Sheet1”的所有行,依此类推。

我可以创建表格,但不能将数据放入其中,有人可以帮忙吗?以下是我到目前为止的代码:

sheets = ['Sheet1', 'Sheet10', 'Sheet2', 'Sheet3']
finalfile = 'test to check.xlsx'
writer = pd.ExcelWriter(finalfile)
for sheet in sheets:
    df.to_excel(writer,sheet)
writer.save()

【问题讨论】:

  • 无法理解您的问题。你能解释清楚吗?
  • 我有一个包含 3 列的 Excel 文件:工作表、名称、版本。我想为列表sheets 上的每个元素创建一个工作表,并且此工作表的内容来自具有 3 列的 Excel 文件。基本上,当在 A 列“Sheet”中我有“Sheet1”时,我希望所有这些行都在工作表“Sheet1”中。是不是更清楚了?

标签: excel pandas python-2.7


【解决方案1】:

我想您的代码中的以下更正对于您的预期输出应该可以正常工作。

for sheet in sheets:
    df.loc[(df['Sheet'] == sheet)].to_excel(writer, sheet)

让我知道这是否有效:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-02
    • 2014-07-04
    • 1970-01-01
    • 2020-04-19
    • 1970-01-01
    • 2020-07-17
    相关资源
    最近更新 更多