[英]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.
然后,我获取该列表,并尝试将其另存为.csv文件。 However, I'm getting the following error: TypeError: must be convertible to a buffer, not DataFrame.
但是,我收到以下错误:TypeError:必须可转换为缓冲区,而不是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
: 您正在寻找
DataFrame.to_csv
:
for item in my_list:
item.to_csv(new_file)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.