Categories
Python Answers

How to write a Pandas DataFrame to CSV file with Python?

Spread the love

Sometimes, we want to write a pandas DataFrame to CSV file with Python.

In this article, we’ll look at how to write a pandas DataFrame to CSV file with Python.

How to write a pandas DataFrame to CSV file with Python?

To write a pandas DataFrame to CSV file with Python, w ecan use the to_csv method.

For instance, we write

df.to_csv(file_name, sep='\t', encoding='utf-8')

to call to_csv to write the df data frame’s data to the file at path file_name.

We also set the sep argument to set the item separator.

And we set the encoding argument to set the encoding for the file.

Conclusion

To write a pandas DataFrame to CSV file with Python, w ecan use the to_csv method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *