0

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, i.e. 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?

11
  • IIUC you try remove DataFrame - row.to_csv(path, index=False, mode='w', header=headers) Commented Oct 4, 2016 at 12:00
  • You're doing it wrong. See df.itertuples Commented Oct 4, 2016 at 12:01
  • Firstly why are you doing this? all you're doing is writing the entire df to a location, why not just do df.to_csv(path, index=False)? Commented Oct 4, 2016 at 12:03
  • @jezrael yeah, I've tried it, but then it tells me that there is no to_csv attribute for pandas stackoverflow.com/questions/38566430/… Commented Oct 4, 2016 at 14:38
  • @EdChum Perhaps I am, my intention, however, is to write only the row. Each row I want to write separately to a different csv. The code isn't shown, but the path variable changes for each row Commented Oct 4, 2016 at 14:40

1 Answer 1

0

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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.