Menu Close

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

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.

Posted in Python, Python Answers