乐闻世界logo
搜索文章和话题

How to avoid pandas creating an index in a saved csv

1个答案

1

When saving data to a CSV file using pandas, by default, the index is also saved along with the data.

To avoid including the index in the CSV file, you can use the index=False parameter when calling the to_csv() method.

For example, suppose we have a DataFrame df and we want to save it to a CSV file without the index column. We can do this:

python
import pandas as pd # Assuming df is already created df.to_csv('output.csv', index=False)

This way, the generated CSV file will not include the original DataFrame's index column.

Using index=False is a direct and commonly used approach for such requirements. It helps keep the data clean, especially when the index information is not practically useful for subsequent data processing and analysis. Additionally, it helps reduce file size, making the file more compact.

2024年7月20日 14:44 回复

你的答案