【问题标题】:Export from pandas to_excel without row names (index)?从没有行名(索引)的熊猫 to_excel 导出?
【发布时间】:2014-04-01 02:38:45
【问题描述】:

我正在尝试将 pandas 中的数据框打印到 Excel 中。这里我使用 to_excel() 函数。但是,我发现Excel中的第一列是“索引”,

0   6/6/2021 0:00   8/6/2021 0:00
1   4/10/2024 0:00  6/10/2024 0:00
2   4/14/2024 0:00  6/14/2024 0:00

有什么办法可以去掉第一列?

【问题讨论】:

    标签: pandas python-2.7 indexing export-to-excel


    【解决方案1】:

    您需要在to_excel 中设置index=False 以使其不写出索引列,其他Pandas IO 工具遵循此语义,请参阅http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_excel.htmlhttp://pandas.pydata.org/pandas-docs/stable/io.html

    【讨论】:

    • 请注意,如果您有 MultiIndex 列,index=False 将不起作用(截至 Pandas 1.3.4,2021 年 10 月)。 NotImplementedError: Writing to Excel with MultiIndex columns and no index ('index'=False) is not yet implemented.
    【解决方案2】:

    示例:index = False

    import pandas as pd
    
    writer = pd.ExcelWriter("dataframe.xlsx", engine='xlsxwriter')
    dataframe.to_excel(writer,sheet_name = dataframe, index=False)
    writer.save() 
    

    【讨论】:

      猜你喜欢
      • 2017-11-09
      • 1970-01-01
      • 2020-03-13
      • 1970-01-01
      • 1970-01-01
      • 2017-02-17
      相关资源
      最近更新 更多