| by Arround The Web | No comments

Pandas Drop Index

Pandas DataFrames can also be used for manipulating tabular data in Python. They are similar to spreadsheets, where each row and column has a label and a value. Pandas DataFrames contains the index, which is a way of identifying each row in the table. Pandas assign a numerical index to each row by default, starting from “0” and increasing by “1”. However, sometimes we need to remove the index to add other columns as an index. To remove/drop the index the “df.reset_index()” method is used in Python.

This blog will present/offer a detailed tutorial on how to drop the index of Pandas DataFrame in Python.

How to Drop the Index of Python Pandas DataFrame?

To drop/remove the index column in a DataFrame in Python, we can use the “df.reset_index()” method. This method removes the index and resets it to the default Range Index. Here’s an example:

import pandas
data = {
        "Name": ["Joseph", "Jason", "Gloor", "Melissa"],
        "Age": [20, 35, 14, 19],
        "Height": [5.0, 4.8, 5.5, 4.3],
        }
df = pandas.DataFrame.from_dict(data).set_index("Name")
print(df, '\n')
df = df.reset_index(drop=True)
print(df)

 
In this code, the DataFrame is created with the “Name” column as an index of the DataFrame. Next the “df.reset_index()” takes the “drop=True” parameter and drops the index of the DataFrame.

Note: When we use the “df.reset_index()” method, the index is removed and replaced with a new column containing the default Range Index. If we want to remove the index column while saving a CSV file in Pandas, we can pass the index parameter to the “df.to_csv()” method and set it to the “False”.

Output


The index column value named “Name” has been removed successfully.

Dropping an Index in a Multi-Index DataFrame by Keeping Its Value

We can also drop the multiindex DataFrames using the “df.reset_index()” method. The multi-index has more than one column serving as the index. Here is a code that displays/shows how to drop/delete multi-index:

import pandas
df = pandas.DataFrame.from_dict({
    'Name': ['Lily', 'Jon', 'Cyndy', 'Anna'],
    'Age': [21, 23, 14, 13],
    'Height': [4.5, 5.8, 4.5, 4.5],
    'Sex': ['F', 'M', 'F', 'F']}).set_index(['Name', 'Age'])
print(df, '\n')
df = df.reset_index(level='Age')
print(df)

 
In this code, we used the “reset_index()” method with the level parameter to drop only a single index while keeping its values as part of the DataFrame.

Output


The above output shows removing one index column from the multi-index column.

Dropping an Index in a Multi-Index DataFrame Without Keeping Its Value

If we don’t want to keep the value of the index after dropping, then we delete the index using the “drop=True” parameter value. For example, in the below code, the index column dropped is deleted from the DataFrame:

import pandas
df = pandas.DataFrame.from_dict({
    'Name': ['Lily', 'Jon', 'Cyndy', 'Anna'],
    'Age': [21, 23, 14, 13],
    'Height': [4.5, 5.8, 4.5, 4.5],
    'Sex': ['F', 'M', 'F', 'F']}).set_index(['Name', 'Age'])
print(df, '\n')
df = df.reset_index(level='Age', drop=True)
print(df)

 
The index column has been deleted from the multi-index without keeping the value:

Dropping Other Columns Using “DataFrame.drop()” Method

We can also use the drop() method to remove any other columns from the data frame by specifying the column name and the axis argument:

import pandas
data = {
        "Name": ["Joseph", "Jason", "Gloor", "Melissa"],
        "Age": [20, 35, 14, 19],
        "Height": [5.0, 4.8, 5.5, 4.3],
        }
df = pandas.DataFrame.from_dict(data).set_index("Name")
print(df, '\n')
df = df.drop("Height", axis=1)
print(df)

 
The column name “Height” has been removed successfully:

Conclusion

The “DataFrame.reset_index()” method is used along with the “drop=True” parameter to drop/delete the index column of DataFrame. We can use this method along with the level parameter to drop the index of the multi-index DataFrame. The “DataFrame.drop()” method can also be used to drop the other columns of DataFrame. This guide presented detailed information on dropping the index columns in Python.

Share Button

Source: linuxhint.com

Leave a Reply