0

I am encountering a scenario where deleting/update columns of a pandas dataframe is required. What I get as a result is that the columns I am trying to delete, get deleted but when I export a new CSV file from the dataframe, I see new columns added that have the same data + data types of the columns I deleted. These new columns have names like Unnamed :0, Unnamed: 0.1, Unnamed: 0.1.1, etc.

Here is the snippet that represents the procedure:

import pandas as pd
import numpy as np

df = pd.read_csv('./<some_file>.csv', encoding='unicode_escape')
del df['col']
df.to_csv('./<some_file>.csv')

df = pd.read_csv('./<some_file>.csv', encoding='unicode_escape')

Any ideas why I am encountering such a situation and whether what I am doing is correct?

Ali H. Kudeir
  • 756
  • 2
  • 9
  • 19
  • 2
    In addition to Anurag's `to_csv` answer, you can also specify the `index_col` on `read_csv`: see [How to get rid of “Unnamed: 0” column in a pandas DataFrame?](https://stackoverflow.com/a/54358758/13138364) – tdy Mar 18 '21 at 17:37

1 Answers1

0

Possibly this is because you are storing index in your csv file and after saving and loading that file it makes a new index and the previous index is turned into names like Unnamed :0, Unnamed: 0.1, Unnamed: 0.1.1

so to solve this just pass index parameter in to_csv() method and set that equal to False:-

df.to_csv('./<some_file>.csv',index=False)
Anurag Dabas
  • 23,866
  • 9
  • 21
  • 41