To drop an index column in Pandas, you can use the reset_index()
function.
This function will convert the index of a dataframe into a column, and then you can use the drop()
function to drop that column.
Here's an example of using the reset_index()
and drop()
functions in Pandas to drop an index column:
import pandas as pd
# create a sample dataframe
df = pd.DataFrame({"A": [1, 2, 3, 4, 5],
"B": [6, 7, 8, 9, 10]},
index=["a", "b", "c", "d", "e"])
# drop the index column
df_no_index = df.reset_index().drop(columns=["index"])
# display the result
print(df_no_index)
This will convert the index of the dataframe into a column named "index", and then drop that column using the drop()
function.
The output will be:
A B
0 1 6
1 2 7
2 3 8
3 4 9
4 5 10
Related tutorials curated for you
How to shuffle data in Pandas
How to convert a series to a list in Pandas
What is Pandas Cumsum()?
How to add an empty column to a Pandas DataFrame
How to use str.contains() in Pandas
How to get the absolute value for a column in Pandas
How to drop duplicate rows in Pandas
How to use qcut() in Pandas
How to create a freqeuncy table in Pandas
How to create a bar chart in Pandas
What is categorical data in Pandas?
How to find the mode in a Pandas DataFrame