197

I have the following code which imports a CSV file. There are 3 columns and I want to set the first two of them to variables. When I set the second column to the variable "efficiency" the index column is also tacked on. How can I get rid of the index column?

df = pd.DataFrame.from_csv('Efficiency_Data.csv', header=0, parse_dates=False)
energy = df.index
efficiency = df.Efficiency
print efficiency

I tried using

del df['index']

after I set

energy = df.index

which I found in another post but that results in "KeyError: 'index' "

Monica Heddneck
  • 2,973
  • 10
  • 55
  • 89
Bogdan Janiszewski
  • 2,743
  • 3
  • 20
  • 25

10 Answers10

375

When writing to and reading from a CSV file include the argument index=False and index_col=False, respectively. Follows an example:

To write:

 df.to_csv(filename, index=False)

and to read from the csv

df.read_csv(filename, index_col=False)  

This should prevent the issue so you don't need to fix it later.

Community
  • 1
  • 1
Steve
  • 4,388
  • 3
  • 17
  • 25
127

df.reset_index(drop=True, inplace=True)

Asclepius
  • 57,944
  • 17
  • 167
  • 143
Subhojit Mukherjee
  • 1,355
  • 1
  • 9
  • 2
  • 3
    This is actually my favorite solution, but not a very elaborate answer. The manual reads this about the argument `drop`: "Do not try to insert index into dataframe columns. This resets the index to the default integer index." https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.reset_index.html – tommy.carstensen Aug 31 '18 at 18:59
  • @tommy.carstensen Then how would you avoid getting the integers on the index as a replacement of the previous index? I think it is a misunderstanding of the text of your link. The question here *is to drop the index*. And this is reached here. You get the default integers, since there is no dateframe without an index, but you have dropped the previous index. That is why this answer should be the accepted answer, also because it uses the memory efficient `inplace=True`. – questionto42 Jul 31 '20 at 13:10
89

DataFrames and Series always have an index. Although it displays alongside the column(s), it is not a column, which is why del df['index'] did not work.

If you want to replace the index with simple sequential numbers, use df.reset_index().

To get a sense for why the index is there and how it is used, see e.g. 10 minutes to Pandas.

Jean-François Corbett
  • 37,420
  • 30
  • 139
  • 188
Dan Allan
  • 34,073
  • 6
  • 70
  • 63
21

You can set one of the columns as an index in case it is an "id" for example. In this case the index column will be replaced by one of the columns you have chosen.

df.set_index('id', inplace=True)
Natheer Alabsi
  • 2,790
  • 4
  • 19
  • 28
6

If your problem is same as mine where you just want to reset the column headers from 0 to column size. Do

df = pd.DataFrame(df.values);

EDIT:

Not a good idea if you have heterogenous data types. Better just use

df.columns = range(len(df.columns))
Bhanu Pratap Singh
  • 1,017
  • 1
  • 12
  • 15
3

you can specify which column is an index in your csv file by using index_col parameter of from_csv function if this doesn't solve you problem please provide example of your data

yemu
  • 26,249
  • 10
  • 32
  • 29
3

One thing that i do is df=df.reset_index() then df=df.drop(['index'],axis=1)

Lord Varis
  • 57
  • 1
  • Error: "labels ['index'] not contained in axis" – Vasin Yuriy Nov 11 '19 at 12:25
  • @VasinYuriy this is meant like `df.reset_index().drop(columns=['yourfirstindex', 'yoursecondindex'])`, it works with 'index' only in the standard case that the index does not have a name and then becomes a column called 'index' with `df.reset_index().drop(columns=['index'])`. The added parameter `axis=1` is the default. This method is not recommended, @SubhojitMukherjee's `reset_index(inplace=True)` works "inplace" and thus saves memory. – questionto42 Jul 31 '20 at 12:59
3

To remove or not to create the default index column, you can set the index_col to False and keep the header as Zero. Here is an example of how you can do it.

recording = pd.read_excel("file.xls",
                     sheet_name= "sheet1",
                     header= 0,
                     index_col= False)

The header = 0 will make your attributes to headers and you can use it later for calling the column.

Ali Taheri
  • 116
  • 3
1

I tried index_col=False, and index_col=None, from the answers posted for this question but none worked.
But index_col=0 worked.

So do like this when reading a file if you want to drop the unwanted index column.
df = pd.read_csv('filename.csv', index_col=0)

007mrviper
  • 459
  • 4
  • 20
0

It works for me this way:

Df = data.set_index("name of the column header to start as index column" )