Home » How to Drop the Index Column in Pandas (With Examples)

How to Drop the Index Column in Pandas (With Examples)

by Erma Khan

Occasionally you may want to drop the index column of a pandas DataFrame in Python.

Since pandas DataFrames and Series always have an index, you can’t actually drop the index, but you can reset it by using the following bit of code:

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

For example, suppose we have the following pandas DataFrame with an index of letters:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'points': [25, 12, 15, 14, 19, 23, 25, 29],
                   'assists': [5, 7, 7, 9, 12, 9, 9, 4],
                   'rebounds': [11, 8, 10, 6, 6, 5, 9, 12]})

#set index of DataFrame to be random letters
df = df.set_index([pd.Index(['a', 'b', 'd', 'g', 'h', 'm', 'n', 'z'])])

#display DataFrame
df

        points	assists	 rebounds
a	25	5	 11
b	12	7	 8
d	15	7	 10
g	14	9	 6
h	19	12	 6
m	23	9	 5
n	25	9	 9
z	29	4	 12

We can use the reset_index() function to reset the index to be a sequential list of numbers:

#reset index
df.reset_index(drop=True, inplace=True)

#display DataFrame
df

        points	assists	 rebounds
0	25	5	 11
1	12	7	 8
2	15	7	 10
3	14	9	 6
4	19	12	 6
5	23	9	 5
6	25	9	 9
7	29	4	 12

Notice that the index is now a list of numbers ranging from 0 to 7.

As mentioned earlier, the index is not actually a column. Thus, when we use the shape command, we can see that the DataFrame has 8 rows and 3 columns (as opposed to 4 columns):

#find number of rows and columns in DataFrame
df.shape

(8, 3)

Bonus: Drop the Index When Importing & Exporting

Often you may want to reset the index of a pandas DataFrame after reading it in from a CSV file. You can quickly reset the index while importing it by using the following bit of code:

df = pd.read_csv('data.csv', index_col=False) 

And you can make sure that an index column is not written to a CSV file upon exporting by using the following bit of code:

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

Additional Resources

How to Set Column as Index in Pandas
How to Drop Rows with NaN Values in Pandas
How to Sort Values in a Pandas DataFrame

Related Posts