Introduction and Lesson Overviews

Welcome, future data analyzers! Today, we're tackling Index Columns and Locating Elements in a Pandas DataFrame. We'll learn how to handle index columns, locate specific data, and strengthen our understanding of DataFrames. Ready, set, code!

Understanding the Index Column in a Pandas DataFrame

In a Pandas DataFrame, an index is assigned to each row, much like the numbers on books in a library. When a DataFrame is created, Pandas establishes a default index. Let's refer to an example:

import pandas as pd

data = {
    "Name": ["John", "Anna", "Peter", "Linda"],
    "Age": [28, 24, 35, 32],
    "City": ["New York", "Paris", "Berlin", "London"]
}

df = pd.DataFrame(data)

print(df)
"""Output:
    Name  Age      City
0   John   28  New York
1   Anna   24     Paris
2  Peter   35    Berlin
3  Linda   32    London
"""

The numbers on the left are the default index.

Setting and Modifying the Index Column

Occasionally, we might need to establish a custom index. The Pandas' set_index() function allows us to set a custom index. To reset the index to its default state, we use reset_index().

To better understand these functions, let's consider an example in which we create an index using unique IDs:

df['ID'] = [101, 102, 103, 104]    # Adding unique IDs
df.set_index('ID', inplace=True)   # Setting 'ID' as index

print(df)
"""Output:
      Name  Age      City
ID                       
101   John   28  New York
102   Anna   24     Paris
103  Peter   35    Berlin
104  Linda   32    London
"""

In this example, ID column is displayed as an index. Let's reset the index to return to the original state:

df.reset_index(inplace=True)       # Resetting index

print(df)
"""Output:
    ID   Name  Age      City
0  101   John   28  New York
1  102   Anna   24     Paris
2  103  Peter   35    Berlin
3  104  Linda   32    London
"""

By setting inplace parameter to True, we ask pandas to reset the index in the original df dataframe. Otherwise, pandas will create a copy of the data frame with a reset index, leaving the original df untouched.

Locating Elements in a DataFrame

Let's consider a dataframe with a custom index. If you want to select a specific row based on its index value (for example, ID = 102), you can do this:

import pandas as pd

data = {
    "Name": ["John", "Anna", "Peter", "Linda"],
    "Age": [28, 24, 35, 32],
    "City": ["New York", "Paris", "Berlin", "London"]
}

df = pd.DataFrame(data)
df['ID'] = [101, 102, 103, 104]    # Adding unique IDs
df.set_index('ID', inplace=True)   # Setting 'ID' as index

print(df.loc[102])
'''Output:
Name     Anna
Age        24
City    Paris
Name: 102, dtype: object
'''
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