Reading an Excel file in python using pandas

Rak*_*van 134 python python-2.7 pandas

I am trying to read an excel file this way :

newFile = pd.ExcelFile(PATH\FileName.xlsx)
ParsedData = pd.io.parsers.ExcelFile.parse(newFile)
Run Code Online (Sandbox Code Playgroud)

which throws an error that says two arguments expected, I don't know what the second argument is and also what I am trying to achieve here is to convert an Excel file to a DataFrame, Am I doing it the right way? or is there any other way to do this using pandas?

DSM*_*DSM 207

关闭:首先调用ExcelFile,然后调用.parse方法并将其传递给工作表名称.

>>> xl = pd.ExcelFile("dummydata.xlsx")
>>> xl.sheet_names
[u'Sheet1', u'Sheet2', u'Sheet3']
>>> df = xl.parse("Sheet1")
>>> df.head()
                  Tid  dummy1    dummy2    dummy3    dummy4    dummy5  \
0 2006-09-01 00:00:00       0  5.894611  0.605211  3.842871  8.265307   
1 2006-09-01 01:00:00       0  5.712107  0.605211  3.416617  8.301360   
2 2006-09-01 02:00:00       0  5.105300  0.605211  3.090865  8.335395   
3 2006-09-01 03:00:00       0  4.098209  0.605211  3.198452  8.170187   
4 2006-09-01 04:00:00       0  3.338196  0.605211  2.970015  7.765058   

     dummy6  dummy7    dummy8    dummy9  
0  0.623354       0  2.579108  2.681728  
1  0.554211       0  7.210000  3.028614  
2  0.567841       0  6.940000  3.644147  
3  0.581470       0  6.630000  4.016155  
4  0.595100       0  6.350000  3.974442  
Run Code Online (Sandbox Code Playgroud)

你正在做的是调用生活在类本身上的方法,而不是实例,这是好的(尽管不是非常惯用),但如果你这样做,你还需要传递工作表名称:

>>> parsed = pd.io.parsers.ExcelFile.parse(xl, "Sheet1")
>>> parsed.columns
Index([u'Tid', u'dummy1', u'dummy2', u'dummy3', u'dummy4', u'dummy5', u'dummy6', u'dummy7', u'dummy8', u'dummy9'], dtype=object)
Run Code Online (Sandbox Code Playgroud)

  • 我发现需要**pip install xlrd**才能工作.xlrd包没有pandas,所以如果你没有为其他目的安装它,你会得到一个"ImportError:No module named xlrd"异常.无论如何,Mac上的pandas 0.19.0都是如此. (8认同)
  • 当我使用"df = xl.parse("Sheet1")"它会自动将每列的第一个单元格的值作为数据框的列名称时,如何指定自己的列名? (6认同)
  • 在pandas 15.0.2中,`parsed = pd.io.parsers.ExcelFile.parse(xl,"Sheet1")`不起作用并抛出错误`模块对象没有属性ExcelFile`.`parsed = pd.io.excel.ExcelFile.parse(xl,"Sheet1")`对我有用 (2认同)
  • 你怎么防止它把第一行变成标题?我已经尝试使用参数`headers = None`但是它没有破坏代码,它也没有用. (2认同)

Mur*_*uru 88

这是一种简单易行的方法.

import pandas
df = pandas.read_excel(open('your_xls_xlsx_filename','rb'), sheetname='Sheet 1')
# or using sheet index starting 0
df = pandas.read_excel(open('your_xls_xlsx_filename','rb'), sheetname=2)
Run Code Online (Sandbox Code Playgroud)

查看文档的完整详细信息 http://pandas.pydata.org/pandas-docs/version/0.17.1/generated/pandas.read_excel.html

FutureWarning:sheetname对于较新的Pandas版本,不推荐使用该关键字,而是使用该关键字sheet_name.


Dr *_*tan 19

我想在此添加,如果你想访问行或列来循环它们,你可以这样做:

import pandas as pd

# open the file
xlsx = pd.ExcelFile(PATH\FileName.xlsx)

# get the first sheet as an object
sheet1 = xlsx.parse(0)

# get the first column as a list you can loop through
# where the is 0 in the code below change to the row or column number you want    
column = sheet1.icol(0).real

# get the first row as a list you can loop through
row = sheet1.irow(0).real
Run Code Online (Sandbox Code Playgroud)


Aja*_*ant 12

我认为这应该满足你的需要:

import pandas as pd

# Read the excel sheet to pandas dataframe
DataFrame = pd.read_excel("PATH\FileName.xlsx", sheetname=0)
Run Code Online (Sandbox Code Playgroud)

  • 仅供参考,正确的参数名称是“sheet_name”而不是“sheetname”。 (6认同)