2016-10-04 56 views
0

我試圖用itertuples()循環內的pandas.DataFrame中的to_csv方法將數據幀對象中的每一行寫入新的csv。但是,每當我在行對象上調用DataFrame時,我會得到以下錯誤。AttributeError:'熊貓'的對象沒有屬性'數據框'

AttributeError的:「熊貓」對象有沒有屬性「數據幀」

關於同樣的錯誤的其他問題表明,這是要麼做:數據幀的

1)誤資本化,即數據幀,或Dataframe 2)或熊貓本身的問題,在安裝過程中出現錯誤等。

我知道它不是1),因爲我已經將它寫入DataFrame中,如錯誤消息中所示。此外,我不期望它是2),因爲我可以運行pandas.read_csv來導入數據框,然後在這些對象上運行方法沒有任何問題。

所以,我的問題是:

1)有另一種可能的來源的問題,也許是從嘗試應用該方法在一個循環連續獲得?

2)如何驗證熊貓及其所有方法安裝正確?所以我可以消除2)作爲一種可能性。

for row in df.itertuples(): 
     if not os.path.isfile(path): 
      row.DataFrame.to_csv(path, index=False, mode='w', header=headers) 
     elif os.path.isfile(path): 
      row.DataFrame.to_csv(path, index=False, mode='a') 

AttributeError       Traceback (most recent call last) 
<ipython-input-192-0af973f1c147> in <module>() 
    39       row.DataFrame.to_csv(path, index=False, mode='w 
, header=headers) 
    40     elif os.path.isfile(path): 
---> 41       row.DataFrame.to_csv(path, index=False, mode='a 
) 

AttributeError: 'Pandas' object has no attribute 'DataFrame' 

我試過消除了itertuples()循環,並用應用於數據框的函數進行替換。功能是:

df.apply(lambda x: df.loc[x].to_csv(''.join([dir,'-'.join([df.iloc[x][3],df.iloc[x][5],df.iloc[x][4],df.iloc[x][0]]),'.csv']) 

嵌套連接方法組成每行內值的路徑。我測試了這個各種行,它工作正常,但現在我收到以下錯誤就行與功能:

type error: ('unorderable types: str() >= int()', 'occurred at index 0') 

這是什麼意思?它試圖命令什麼?爲什麼?

+0

IIUC您嘗試刪除'DataFrame' - 'row.to_csv(路徑,指數=假,模式= 'W',標題=標題)' – jezrael

+0

你就錯了。參見['df.itertuples'](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.itertuples.html) –

+0

首先,你爲什麼要這樣做?你所要做的就是將整個df寫入一個位置,爲什麼不只是'df.to_csv(path,index = False)'? – EdChum

回答

0

我嘗試將Outlook郵件數據加載到數據框時遇到過相同的問題。 在下面的代碼中,如果輸入數據丟失/ 消息,您可以找到一個簡單的方法來解決。在本示例中爲 /。

import win32com.client 
import pandas as pd 
outlook =win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI") 
inbox = outlook.GetDefaultFolder(5) 
messages = inbox.Items 
message = messages.GetFirst() 
df2 = pd.DataFrame([[' ',' ',' ']],columns=list('ABC')) 
i=1 
while message: 
    try: 
     df2.loc[i]=(message.Subject,message.SenderEmailAddress,message.To) 
     i=i+1 
    except: 
     print("Error!") 
     df2.loc[i]=(message.Subject,message.SenderEmailAddress,"Üres") 
     i=i+1 
    message = messages.GetNext() 
相關問題