Removing index column in pandas when reading a csv

PythonPandas

Python Problem Overview


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

Python Solutions


Solution 1 - Python

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.

Solution 2 - Python

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

Solution 3 - Python

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.

Solution 4 - Python

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)

Solution 5 - Python

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

Solution 6 - Python

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

Solution 7 - Python

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

Solution 8 - Python

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBogdan JaniszewskiView Question on Stackoverflow
Solution 1 - PythonSteveView Answer on Stackoverflow
Solution 2 - PythonSubhojit MukherjeeView Answer on Stackoverflow
Solution 3 - PythonDan AllanView Answer on Stackoverflow
Solution 4 - PythonNatheer AlabsiView Answer on Stackoverflow
Solution 5 - PythonBhanu Pratap SinghView Answer on Stackoverflow
Solution 6 - PythonyemuView Answer on Stackoverflow
Solution 7 - PythonLord VarisView Answer on Stackoverflow
Solution 8 - PythonAli TaheriView Answer on Stackoverflow