【问题标题】:How to read a excel file without taking its first row as header ? Pandas, Python [duplicate]如何在不将第一行作为标题的情况下读取 excel 文件?熊猫,Python [重复]
【发布时间】:2020-05-04 22:13:49
【问题描述】:

优秀的

  A      B      C   
1 apple  tometo grape
2 beer   wine   juice       

pandas读取,第一行是DataFrame的列。

  apple  tometo grape
0 beer   wine   juice       

我怎样才能像这样读取excel:

  0      1      2
0 apple  tometo grape
1 beer   wine   juice       

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    可以使用文件名作为字符串或打开的文件对象来读取文件:

    pd.read_excel('test.xlsx', index_col=0)
    

    如果您想阅读特定的表格。

    pd.read_excel(open('test.xlsx', 'rb'),sheet_name='Sheet3')
    

    可以通过 index_col 和 header 参数指定索引和标题

    pd.read_excel('test.xlsx', index_col=None, header=None)
    

    【讨论】:

    • 嘿,Jay,pd.read_excel('test.xlsx', index_col=None, header=None) 解决了我的问题,谢谢!
    【解决方案2】:

    当你搜索“pandas read excel file skip header”时,谷歌上的第一个答案:Pandas doc for method read_excel which has a skiprows argument to implement what you like

    由于你的excel文件没有标题行,你应该使用header=None

    【讨论】:

    • 嘿Emrah,感谢您的帮助。我已经尝试过了,skiprows 只是省略行,我想要一个新行作为每个列的标签。
    • 对不起,我误解了你的问题,我更新了我的答案
    猜你喜欢
    • 1970-01-01
    • 2022-01-04
    • 2023-01-16
    • 1970-01-01
    • 1970-01-01
    • 2021-10-11
    • 1970-01-01
    • 2018-11-01
    • 1970-01-01
    相关资源
    最近更新 更多