0 votes
1 view
in Python by (47.8k points)

I am trying to save a CSV to a folder after making some edits to the file.

Every time I use pd.to_csv('C:/Path of file.csv') the CSV file has a separate column of indexes. I want to avoid printing the index to CSV.

I tried:

pd.read_csv('C:/Path to file to edit.csv', index_col = False)

And to save the file...

pd.to_csv('C:/Path to save edited file.csv', index_col = False)

However, I still got the unwanted index column. How can I avoid this when I save my files?

1 Answer

0 votes
by (107k points)

There are many ways to avoid Python/Pandas creating an index in a saved CSV some of the important ways are as follows:-

The first and most preferable way would be to set your index value as index=False while you are converting your data frame into CSV below is an example that shows how to do it.

df.to_csv('your.csv', index=False)

Another way to solve the above problem would be, you can save your dataframe as it is with an index, and while reading the columns you just need to drop the column by providing the value unnamed 0 which contains your previous index. Below is an example regarding the sam that will guide you on how to do it:-

df.to_csv(' file_name.csv ')

df_new_idx = pd.read_csv('file_name.csv').drop(['unnamed 0'],axis=1)

Welcome to Intellipaat Community. Get your technical queries answered by top developers !