简体   繁体   中英

Printing dataframe to .csv in python. TypeError:must be convertible to a buffer

I'm going through a dataframe and appending specific rows into a list. I'm then taking that list and trying to save it as a .csv file. However, I'm getting the following error: TypeError: must be convertible to a buffer, not DataFrame.

Any suggestions on what this is and how to fix this is greatly appreciated.

Here's some code:

my_list = []
my_list.append(df)

CSVdir = r"C:\Users\...."
realCSVdir = os.path.realpath(CSVdir)

if not os.path.exists(CSVdir):
    os.makedirs(CSVdir)


new_file_name = os.path.join(realCSVdir,'banana.csv')
new_file = open(new_file_name, 'wb')

for item in my_list:
    new_file.write(item)
    new_file.write("\n")

You're looking for DataFrame.to_csv :

for item in my_list:
    item.to_csv(new_file)

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