简体   繁体   English

在python中将数据帧打印到.csv。 TypeError:必须转换为缓冲区

[英]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.

相关问题 Python TypeError:必须可转换为缓冲区,而不是OrderedDict - Python TypeError: must be convertible to a buffer, not OrderedDict TypeError:参数1必须可转换为缓冲区,而不是BeautifulSoup - TypeError: argument 1 must be convertible to a buffer, not BeautifulSoup python字符串模板:错误“必须可转换为缓冲区,而不是模板” - python string template : Error 'must be convertible to a buffer, not Template' 必须可转换为缓冲区,而不是QueryDict Django - must be convertible to a buffer, not QueryDict Django Python:将数据框打印到csv - Python: Printing dataframe to csv Python:将数据框写入并打印为CSV - Python: Writing and printing to CSV a dataframe Python打印对象 - TypeError:必须是str,而不是Address - Python printing objects - TypeError: must be str, not Address 带有re和csv的Python“ TypeError:预期的字符串或缓冲区” - Python “TypeError: expected string or buffer” with re and csv 在 python 中打印不可转换为列表的 map object 的结果 - Printing the result of a map object in python that is not convertible to list 在 Python 中创建交叉表时出错。 TypeError:列表索引必须是整数或切片,而不是元组 - Error creating a Crosstab in Python. TypeError: list indices must be integers or slices, not tuple
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM