从excell

时间:2017-02-24 10:53:44

标签: python pandas

我正在尝试从每张纸上获取数据并按顺序将它们添加到工作表中。

这是我的代码:

import os
import pandas as pd
xl = pd.ExcelFile("D:/rain.xls")
excell_names=xl.sheet_names.

df=pd.DataFrame()
for i in excell_names:
    ex=str(i)
    data=pd.read_excel(int(i),"sheet1")
    df=df.append(data)
    print df

以下是Excel中的工作表:

[u'17162', u'17196', u'17248', u'17250', u'17255',
 u'17340', u'17351', u'17370', u'17710', u'17762',
 u'17802', u'17836', u'17837', u'17840', u'17841',
 u'17866', u'17868', u'17906', u'17907', u'17908',
 u'17934', u'17936', u'17960', u'17978', u'17979',
 u'17981', u'18056', u'18060', u'18139', u'18156',
 u'18184', u'18214', u'18269', u'18455', u'18459',
 u'184502']

以下是错误消息:

C:\Users\owrasa\AppData\Local\Enthought\Canopy\User\Scripts\python.exe C:/Users/owrasa/PycharmProjects/den/pandass.py
Traceback (most recent call last):
  File "C:/Users/owrasa/PycharmProjects/den/pandass.py", line 9, in <module>
    data=pd.read_excel(int(i),"sheet1")
  File "C:\Users\owrasa\AppData\Local\Enthought\Canopy\User\lib\site-packages\pandas\io\excel.py", line 191, in read_excel
    io = ExcelFile(io, engine=engine)
  File "C:\Users\owrasa\AppData\Local\Enthought\Canopy\User\lib\site-packages\pandas\io\excel.py", line 251, in __init__
    raise ValueError('Must explicitly set engine if not passing in'
ValueError: Must explicitly set engine if not passing in buffer or path for io.

Process finished with exit code 1

但通常,他们的名字是17162,17196,每个都有相同的列。那我该怎么做呢?

2 个答案:

答案 0 :(得分:1)

试试这个:

import pandas as pd
filename = "D:/rain.xls"
xl = pd.ExcelFile(filename)
excell_names=xl.sheet_names
dfs = pd.read_excel(filename,sheetname=None)
df = pd.concat([dfs[excelname] for excelname in excell_names])
print(df)

答案 1 :(得分:0)

此代码也可以解决此问题:

#Read and write to excel
dataFileUrl = R"D:\\real_names.xlsx"
data = pd.read_excel(dataFileUrl)