211

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' "

10 Answers 10

400

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.

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks a lot.This is exactly what is the question is looking for.
"header = False" works for removing headers in the same way
should be index_col=False.
Using df.to_sql("table",cursor,if_exists="append",index=False) also fixes the sqlite error sqlite3.OperationalError: table message has no column named index
@vedda it seems to be index=False for to_excel() and index_col=False with read_csv() in pandas 0.23.4. :-/
|
145
df.reset_index(drop=True, inplace=True)

2 Comments

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." pandas.pydata.org/pandas-docs/stable/generated/…
@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.
90

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.

6 Comments

Thanks! I decided to just import it a different way not using pandas. I have to perform some arithmetic on each of the columns and python wasn't liking have the index column attached. Pandas is certainly the easiest way to import data but not always the best I found out.
Did you try using Pandas to do the arithmetic?
can one remove the index name?
Yes, index.name = None.
Yes, clearly the next answer should be the accepted one.
|
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)

1 Comment

Hmm, this didn't work for me. I got "None" as a console printout.
8

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))

1 Comment

This is what I wanted, thanks. My situation was where I'd import a text file with sep=' ' that led to extra na columns. On further research I killed the problem earlier now and for others, sep=' ' does not equate to delim_whitespace=True....
5

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)

Comments

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

Comments

3

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

2 Comments

Error: "labels ['index'] not contained in axis"
@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.
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.

Comments

-1

It works for me this way:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.