【问题标题】:Pandas printing column of order when export to excel [duplicate]导出到excel时Pandas打印订单列[重复]
【发布时间】:2020-01-25 01:53:47
【问题描述】:

我将一些数据从 Python/pandas 导出到 Excel,其中包含一个表示订单的列。示例:


+----+---------+------------------+------+------+---------+
|    | Name    | Task             | Team | Date | Month   |
+----+---------+------------------+------+------+---------+
| 0  | John    | Market study     | A    | 1    | Month 1 |
+----+---------+------------------+------+------+---------+
| 1  | Michael | Customer service | B    | 1    | Month 1 |
+----+---------+------------------+------+------+---------+
| 2  | Joanna  | Accounting       | C    | 1    | Month 1 |
+----+---------+------------------+------+------+---------+
| 3  | John    | Accounting       | B    | 2    | Month 1 |
+----+---------+------------------+------+------+---------+
| 4  | Michael | Customer service | A    | 2    | Month 1 |
+----+---------+------------------+------+------+---------+
| 5  | Joanna  | Market study     | C    | 2    | Month 1 |
+----+---------+------------------+------+------+---------+
| 6  | John    | Market study     | A    | 1    | Month 3 |
+----+---------+------------------+------+------+---------+
| 7  | Michael | Customer service | B    | 1    | Month 3 |
+----+---------+------------------+------+------+---------+
| 8  | Joanna  | Accounting       | C    | 1    | Month 3 |
+----+---------+------------------+------+------+---------+
| 9  | John    | Customer service | A    | 2    | Month 3 |
+----+---------+------------------+------+------+---------+
| 10 | Michael | Accounting       | B    | 2    | Month 3 |
+----+---------+------------------+------+------+---------+

问题是当我将文件重新导入pandas时,对于我的计算机,最左边代表顺序的列没有被考虑在内;而对其他人来说,确实如此。为什么,解决方案是什么?

【问题讨论】:

  • 你能澄清一下你的说法吗?您的 excel 文件是否有索引列(最左边的列),当您使用 pandas 导入它时它会消失吗?
  • 嗨,我的代码没有索引列。但是,当我导出到 excel 时,它确实如此。当我导入同一个 excel 文件时,对于某些人来说,它会将索引列加载为数据框中的列;但是对于我的电脑却没有。
  • 导出到 excel 时设置 index=False。 df.to_excel(index=False)
  • “我将一些代码导出到 excel(格式)” 错误,没有。您将一些 数据 导出到 Excel。而且你没有告诉我们你从哪里导出它,我以为你的意思是一些电子表格包,但你的意思是“从熊猫导出”。我们不知道你是从哪里导出的。你必须陈述你的问题。
  • 最左边的“代表顺序的列”称为索引。所以你的问题是“当我将数据(从熊猫)导出到Excel时,索引被删除”或者“当我将Excel文件读入pandas时,索引被删除”.

标签: python excel pandas dataframe export-to-excel


【解决方案1】:

使用df.to_excel(file.xlsx, index=False) 保存到没有索引的file.xlsx

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    相关资源
    最近更新 更多