2016-11-21 52 views
0

系統: Windows 7的 蟒蛇 - Python的AttributeError的,whein從IPython的書面方式回Excel與大熊貓

>的Spyder與2.7.12我得到這個AttributeError的:

File "<ipython-input-4-d258b656588d>", line 1, in <module> 
 
    runfile('C:/xxx/.spyder/pandas excel.py', wdir='C:/xxx/.spyder') 
 

 
    File "C:\xxx\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile 
 
    execfile(filename, namespace) 
 

 
    File "C:\xxx\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py", line 87, in execfile 
 
    exec(compile(scripttext, filename, 'exec'), glob, loc) 
 

 
    File "C:/xxx/.spyder/pandas excel.py", line 33, in <module> 
 
    moving_avg.to_excel(writer, sheet_name='Methodentest', startcol=12, startrow=38) 
 

 
    File "C:\xxx\Anaconda2\lib\site-packages\pandas\core\generic.py", line 2672, in __getattr__ 
 
    return object.__getattribute__(self, name) 
 

 
AttributeError: 'Series' object has no attribute 'to_excel'

這是我的代碼:

import pandas as pd 
 

 
#Adjustmend of Data for Date funtioniert nicht? 
 
#parsen = lambda x: pd.datetime.strptime(x, '%Y-%m') 
 

 
#Open new file object 
 
xl = pd.ExcelFile('C:\xxx\Desktop\Beisspieldatensatz.xlsx') 
 
#parse_dates={'Zeit': ['Jahr', 'Monat']}, index_col = 0, date_parser=parsen) 
 

 
#Link to specific sheet 
 
df = xl.parse('Methodentest') 
 

 
#Narrow the data input 
 
df2 = df[['Jahr', 'Monat', 'Umsatzmenge']] 
 

 
#Establishment values under the year 2015 
 
df3 = df2[(df2['Jahr']<2015)] 
 

 
#Execute gleitender Mittelwert History 36 Month or 36 rows   
 
moving_avg = pd.rolling_mean(df3["Umsatzmenge"],36) 
 
print (moving_avg.head()) 
 

 
#Create a pandas excel writer 
 
writer = pd.ExcelWriter(r'C:\xxx\Desktop\Beisspieldatensatz.xlsx', engine='xlsxwriter') 
 

 
# Convert the dataframe to an XlsxWriter Excel object. 
 
moving_avg.to_excel(writer, sheet_name='Methodentest', startcol=12, startrow=38) 
 

 
# Close the Pandas Excel writer and output the Excel file. 
 
writer.save()

我想讀從Excel中IPython中的數據集。在下一步中,我想「削減」我的數據,但這不起作用? (這就是爲什麼我把這部分加入hashtag)。在此之後,我想要做一個像這裏一樣的數學方法,爲未來18個月移動平均值並將這些信息存儲在move_average中。 我的數據集每月從01.2012開始。然後,代碼必須在Excel中以特定的行和列回寫新的數字 - >這裏發生錯誤。

請有人幫我嗎?

回答

0

我想你需要你的Series轉換回DataFrame保存前,嘗試

moving_avg.to_frame().to_excel(writer, sheet_name='Methodentest', startcol=12, startrow=38) 
+0

我試試你的方法和我的整個excel文件內容deleteted。該代碼應該只更新我的Excel文件不會擦除整個內容!你有什麼解決辦法? (計算工作和粘貼在右列/行的內容,但標題和主鍵也顯示出來,我只想看看結果) – 123GuteLaune

+0

對不起,我不知道寫每創建一個完成時創建白紙... – 123GuteLaune