13.2 C
London
Tuesday, July 2, 2024
HomePandas in PythonDataFrame Functions in PythonHow to Drop the Index Column in Pandas (With Examples)

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

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories