简体   繁体   English

从 Python 导出数据表到 CSV

[英]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')

You can use the to_csv function:您可以使用to_csv function:

before.to_csv('file_name.csv')

If you need extra options, you can check the documentation from here .如果您需要额外的选项,可以从此处查看文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM