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?
DataFrame-row.to_csv(path, index=False, mode='w', header=headers)df.itertuplesdf.to_csv(path, index=False)?