0

I'm using dataframes and CSV files to manipulate data. Most of the time, my dataframes, or the one provided by the API I'm using don't have indexes. If they have indexes, especially when writing and reading CSV files, I just remove them by using the name of the column, "unnamed:0".

But this time, to_CSV places indexes in my CSV file without naming the column. So I must use df.drop(df.columns[0], axis=1, inplace=True). But for pandas, the first column is the first named, not the real first one.

I already used index=False, but it just removed an important column instead of not adding indexes.

How can I remove the first column which isn't named and haven't index to find it ?

TheHaricover
  • 47
  • 1
  • 6

3 Answers3

3

Writing pandas dataframes into file via to_csv() method has optional parameter index, which you can set to false to prevent it from writing its own index:

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

Ach113
  • 1,775
  • 3
  • 18
  • 40
  • I should have specified. `index=False` removed my first column. But I needed that column, so I couldn't use this parameter. Thank you for your answer ! – TheHaricover Jan 22 '20 at 11:53
2

Pandas have a lot of optional parameters you can look into the documentation for further information.

To avoid writing index to the CSV file you can use index=False as an optional parameter

df = pd.DataFrame({'name': ['Raphael', 'Donatello'],
                      'mask': ['red', 'purple'],
                      'weapon': ['sai', 'bo staff']})
df.to_csv(index=False)
Khagendra
  • 591
  • 1
  • 5
  • 19
  • I should have specified. index=False removed my first column. But I needed that column, so I couldn't use this parameter. I edited my question and specified it. Thank you for your time anyway :) – TheHaricover Jan 22 '20 at 11:57
0

Use this : df = pd.read_csv("nameOfFile.csv", index_col="nameOfColToUseAsIndex") and put the name of your first column in "nameOfColToUseAsIndex".

TheHaricover
  • 47
  • 1
  • 6