Indexing and Selecting Data in Pandas

Introduction

Hello! Today we're diving into Indexing and Selecting Data in pandas, a crucial part of data manipulation and analysis. Indexing helps us locate data in specific rows while selecting focuses on picking specific columns or cells.

We'll delve into how to select and index data using pandas by walking you through some hands-on examples. Let's begin!

Understanding Indexing: Setting Index

In pandas, an index is more or less the address of your data. By default, pandas assigns integer labels to the rows, but we can set any column as the index. This effectively turns it into an identifier for the rows.

Here's a basic example using pandas DataFrame's set_index(), reset_index(), and rename() methods:

import pandas as pd

df = pd.DataFrame({
  "Name": ["Alice", "Bob", "John"],
  "Age": [25, 22, 30],
  "City": ["New York", "Los Angeles", "Chicago"]
})

df.set_index("Name", inplace=True)
print(df)
    # Output:
    #         Age          City
    # Name                     
    # Alice   25      New York
    # Bob     22   Los Angeles
    # John    30       Chicago

Accessing data using the index is performed with pandas loc[] method for label-based indexing and iloc[] method for integer-based indexing, which we will investigate later.

The inplace parameter is common for a lot of pandas dataframe methods. If inplace is set to True, changes are applied to the target dataframe. Otherwise, the target dataframe will be copied, the copy will be changed and returned.

However, it is important to note that in the pandas 3.0 the `inplace parameter will be omitted, and you will have to do it this way:

df = df.set_index("Name")

Understanding Indexing: Resetting Index

If you want to reset index back to the default, it is done easily with the following method:

df.reset_index(inplace=True)
print(df)
    # Output:
    #     Name  Age          City
    # 0  Alice   25      New York
    # 1    Bob   22   Los Angeles
    # 2   John   30       Chicago

Understanding Indexing: Renaming Index

Renaming the index is simply renaming the corresponding column. It is done with the rename method:

df.rename(columns={"Name": "Student Name", "Age": "Student Age"}, inplace=True)
print(df)
    # Output:
    #   Student Name  Student Age          City
    # 0        Alice           25      New York
    # 1          Bob           22   Los Angeles
    # 2         John           30       Chicago

Here, we provide a dictionary where the key is the old name, and the value is the new name.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal