2017-02-24 155 views
0

我試圖從每張工作表中獲取數據並按順序將它們添加到工作表中。從Excel工作表中獲取數據

這裏是我的代碼:

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並且每個都有相同的列。那我該怎麼做?

回答

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) 
相關問題