简体   繁体   中英

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

I'm trying to write each row of a slice from a dataframe object to a new csv using the to_csv method in pandas.DataFrame within an itertuples() loop. However, whenever I invoke DataFrame on the row object I get the following error.

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

Other questions regarding the same error indicate that this is either do to:

1) mis-capitalization of DataFrame, ie dataframe, or Dataframe 2) or a problem with pandas itself, a error during installation etc.

I know that it is not 1) because I have written it DataFrame, as seen in the error message. Furthermore, I don't expect it to be 2) because I can run pandas.read_csv to import dataframes and then run methods on these objects without any problem.

So, my questions are:

1) is there another possible source to the problem, perhaps deriving from trying to apply the method on a row in a loop?

2) how can I verify that pandas and all its methods are installed properly? so that I can eliminate 2) as a possibility.

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'

I have tried eliminating the itertuples() loop and replacing with a function applied to the data frame. The function is:

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'])

The nested join methods compose the path from values within each row. I have tested this for various rows and it works fine, but now I am getting the following error on the line with the function:

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

What does this mean? What is it trying to order and why?

I had the same experience when tried to load outlook mail data into dataframe. In the code below you can find an easy way to overcome if the input data is missing / message.To in this example/.

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM