Sling Academy
Home/Pandas/Pandas: How to list all row labels in a DataFrame (5 examples)

Pandas: How to list all row labels in a DataFrame (5 examples)

Last updated: February 21, 2024

Introduction

Pandas is a powerful Python library for data manipulation and analysis, offering a diversity of functionalities that enable data scientists to process and transform data efficiently. One common task when working with DataFrames is identifying and listing all the row labels, which is pivotal for data exploration, cleaning, and preprocessing. This tutorial provides comprehensive guidance on how to list all row labels in a Pandas DataFrame, illustrated with 5 practical examples, ranging from basic to advanced techniques.

Listing Row Labels in Pandas

In Pandas, a DataFrame is a two-dimensional, size-mutable, and potentially heterogeneous tabular data structure with labelled axes (rows and columns). Row labels in a DataFrame are known as the index. These labels can be numeric, string, or datetime objects, providing a reference to each row. Before diving into the examples, it’s essential to ensure you have the Pandas library installed:

pip install pandas

Now, let’s embark on our journey through the examples.

Example 1: Basic Listing of Row Labels

Our first example demonstrates the easiest way to list all row labels of a DataFrame. Let’s start by creating a simple DataFrame:

import pandas as pd

df = pd.DataFrame({
   'Name': ['Alice', 'Bob', 'Charlie'],
   'Age': [25, 30, 35]
})
print(df)

This code snippet creates a DataFrame with names and ages of three individuals. To list the row labels, simply access the index attribute of the DataFrame:

print(df.index)

This will output:

RangeIndex(start=0, end=3, step=1)

This output represents a range of integers serving as the default row labels when none are explicitly defined.

Example 2: Custom Row Labels

To further understand row labels, let’s create a DataFrame with custom row labels:

df.set_index(['Name'], inplace=True)
print(df.index)

This code sets the ‘Name’ column as the index, making the names of the individuals the row labels. The output would be:

Index(['Alice', 'Bob', 'Charlie'], dtype='object')

Example 3: Listing Unique Row Labels

When working with larger datasets, duplicate row labels can occur. To list unique row labels, you can use the unique() method on the index:

print(df.index.unique())

This command yields a list of unique row labels, effectively filtering out any duplicates.

Example 4: Using a Conditional to Filter Row Labels

This example demonstrates how to list row labels based on a condition applied to another column. First, let us revert to default indices and add a column:

df.reset_index(inplace=True)
df['Gender'] = ['F', 'M', 'M']

df.set_index('Gender', inplace=True)
print(df.index[df.index == 'M'])

This code lists row labels, in this case, ‘Gender’, where the condition (gender equals ‘M’) is met. The result is:

Index(['M', 'M'], dtype='object', name='Gender')

Example 5: Advanced Multi-level Row Label Listing

Finally, let’s explore a more complex scenario where a DataFrame has multiple levels of row labels, also known as a MultiIndex. To achieve this:

df = pd.DataFrame({
   'Gender': ['M', 'F', 'M', 'F', 'M'],
   'Employed': ['Y', 'N', 'Y', 'N', 'N'],
   'Name': ['Bob', 'Alice', 'Charlie', 'Diana', 'Eric']
}).set_index(['Gender', 'Employed'])
print(df.index)

This code results in a DataFrame indexed by both ‘Gender’ and ‘Employed’, showcasing a MultiIndex. The output:

MultiIndex([('M', 'Y'),
            ('F', 'N'),
            ('M', 'Y'),
            ('F', 'N'),
            ('M', 'N')],
           names=['Gender', 'Employed'])

Conclusion

Through these examples, we’ve explored various methods to list all row labels in a Pandas DataFrame. Whether dealing with simple or complex DataFrames, understanding how to access and work with row labels is fundamental for data manipulation tasks. Applying these techniques will undoubtedly enhance your data processing workflow.

Next Article: How to view all column labels of a Pandas DataFrame

Previous Article: Pandas: Construct a DataFrame from N Series

Series: DateFrames in Pandas

Pandas

You May Also Like

  • How to Use Pandas Profiling for Data Analysis (4 examples)
  • How to Handle Large Datasets with Pandas and Dask (4 examples)
  • Pandas – Using DataFrame.pivot() method (3 examples)
  • Pandas: How to ‘FULL JOIN’ 2 DataFrames (3 examples)
  • Pandas: Select columns whose names start/end with a specific string (4 examples)
  • 3 ways to turn off future warnings in Pandas
  • How to Integrate Pandas with Apache Spark
  • How to Use Pandas for Web Scraping and Saving Data (2 examples)
  • How to Clean and Preprocess Text Data with Pandas (3 examples)
  • Pandas – Using Series.replace() method (3 examples)
  • Pandas json_normalize() function: Explained with examples
  • Pandas: Reading CSV and Excel files from AWS S3 (4 examples)
  • Using pandas.Series.rank() method (4 examples)
  • Pandas: Dropping columns whose names contain a specific string (4 examples)
  • Pandas: How to print a DataFrame without index (3 ways)
  • Fixing Pandas NameError: name ‘df’ is not defined
  • Pandas – Using DataFrame idxmax() and idxmin() methods (4 examples)
  • Pandas FutureWarning: ‘M’ is deprecated and will be removed in a future version, please use ‘ME’ instead
  • Pandas: Checking equality of 2 DataFrames (element-wise)