Home > Software design >  How to remove Pandas Index Column when Writting to CSV
How to remove Pandas Index Column when Writting to CSV

Time:06-18

Read in CSV string file:

data = pd.read_csv(data, sep=',')

Sorted it in alphabetical order:

 data = data.sort_values("Package", inplace = False)

Reseted the Index:

data = data.reset_index()

Turned off Indexing while writing to CSV:

data.to_csv("combined.csv", index=False)

The Result:

enter image description here

CodePudding user response:

data = data.reset_index(drop=True)

CodePudding user response:

According to this thread, the problem can normally be avoided by skipping calling reset_index.

If that doesn't work for you, one alternative solution that was offered is to try the following:

df.to_csv(' file_name.csv ')
df_new = pd.read_csv('file_name.csv').drop(['unnamed 0'],axis=1)
  • Related