0

I have a data frame 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.

I've tried both csv module and pandas but get errors from both.

The data table has 1765 rows so writing the file row by row is not really an option.

When using pandas, most common errors are

DataFrame constructor not properly called!

and

function object has no attribute '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

iterable expected, not method

Basically, how do I export this table (screenshot attached) into a csv file?

Screenshot of Table

3
  • what error are you getting? And pandas has to_csv for saving a Dataframe to csv Commented May 24, 2020 at 15:51
  • DataFrame constructor not properly called! Commented May 24, 2020 at 15:57
  • 2
    You should update your question with a minimal, reproducible example of your code Commented May 24, 2020 at 15:59

2 Answers 2

1

You can use the to_csv function:

before.to_csv('file_name.csv')

If you need extra options, you can check the documentation from here.

Sign up to request clarification or add additional context in comments.

Comments

1

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,

Try this:

dataframe.to_csv('file_name.csv', header=True, index=False, encoding='utf-8')

Since your dataframe's name is before,

Try this:

before.to_csv('file_name.csv', header=True, index=False, encoding='utf-8')

2 Comments

That worked. Not surprising since I finally used the correct name for my data frame...I think it's time to finally take a break! I'm starting to lose my mind....THANK YOU!
Haha! You're welcome. All the best for your future works!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.