[英]Export Data Table from Python to CSV
I have a data table I created with my original data appended with the topics from topic modeling.我有一个数据表,我创建的原始数据附加了来自主题建模的主题。 I keep running into errors when trying to export the data table into csv.
尝试将数据表导出到 csv 时,我一直遇到错误。
I've tried both csv
module and pandas
but get errors from both.我已经尝试过
csv
模块和pandas
但两者都出现错误。
The data table has 1765 rows so writing the file row by row is not really an option.数据表有 1765 行,因此逐行写入文件并不是一个真正的选择。
When using pandas, most common errors are使用 pandas 时,最常见的错误是
DataFrame constructor not properly called!
DataFrame 构造函数调用不正确!
and和
function object has no attribute 'to_csv'
function object 没有属性“to_csv”
Code used:使用的代码:
import pandas as pd
data = (before.head)
df = pd.DataFrame(before.head)
df.to_csv (r'C:\Users\***\Desktop\beforetopics.csv', index = False, header=True)
print (df)
For the CSV module, there have been several errors such as对于CSV模块,出现了几个错误如
iterable expected, not method
预期可迭代,而不是方法
Basically, how do I export this table (screenshot attached) into a csv file?基本上,我如何将此表(附截图)导出到 csv 文件中?
What is the command that you're trying to run?您尝试运行的命令是什么?
Try this:尝试这个:
dataframe.to_csv('file_name.csv')
Or if it is the unicode error that you're coming across,或者,如果您遇到的是 unicode 错误,
Try this:尝试这个:
dataframe.to_csv('file_name.csv', header=True, index=False, encoding='utf-8')
Since your dataframe's name is before
,由于您的数据框的名称是
before
,
Try this:尝试这个:
before.to_csv('file_name.csv', header=True, index=False, encoding='utf-8')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.