Pandas: Inspect the Axes of a DataFrame (3 Examples)

Updated: February 19, 2024 By: Guest Contributor Post a comment

Introduction

Understanding the structure of a DataFrame is essential for data manipulation and analysis in Pandas. One key aspect of this structure is its axes. In this article, we explore how to inspect the axes of a DataFrame through practical examples.

Understanding DataFrame Axes

In Pandas, a DataFrame is a two-dimensional, size-mutable, and potentially heterogeneous tabular data structure with labeled axes (rows and columns). Axes are one of the fundamental concepts in Pandas, referring to the two dimensions of a DataFrame: the row axis (axis=0) and the column axis (axis=1).

Knowing how to inspect these axes can greatly aid in data cleaning, transformation, and analysis. Let’s dive into some examples to illustrate how you can work with DataFrame axes.

Example 1: Examining Column and Row Labels

import pandas as pd

df = pd.DataFrame({
    'A': [1, 2, 3],
    'B': [4, 5, 6],
    'C': [7, 8, 9]
})

print('Columns:', df.columns)
print('Index (Rows):', df.index)

This basic example demonstrates how to view the column and row labels of a DataFrame. By invoking df.columns and df.index, we immediately get a list of all column names and row indices, respectively.

Example 2: Inspecting the Axis Dimensions

import pandas as pd

df = pd.DataFrame({
    'A': [1, 2, 3],
    'B': [4, 5, 6],
    'C': [7, 8, 9]
})

print('DataFrame Shape:', df.shape)

# To get the size of each axis separately
print('Number of Rows:', df.shape[0])
print('Number of Columns:', df.shape[1])

Output:

DataFrame Shape: (3, 3)
Number of Rows: 3
Number of Columns: 3

This example builds on the first by exploring the dimensions of each axis using the shape attribute. The shape attribute returns a tuple representing the dimensions of the DataFrame, enabling you to quickly ascertain the size of each axis.

Example 3: Customizing Indexes for Detailed Insights

import pandas as pd

df = pd.DataFrame({
    'A': [1, 2, 3],
    'B': [4, 5, 6],
    'C': [7, 8, 9]
})

df.index = pd.date_range('20230101', periods=3)
print('Custom Index (Rows):', df.index)

Output:

Custom Index (Rows): DatetimeIndex(['2023-01-01', '2023-01-02', '2023-01-03'], dtype='datetime64[ns]', freq='D')

In this more advanced example, we customize the DataFrame’s index to provide more detailed insights. By changing the index to a date range, it becomes easier to work with time series data, offering a clear structure for analysis.

Example 4: Iterating over Axes

import pandas as pd

df = pd.DataFrame({
    'A': [1, 2, 3],
    'B': [4, 5, 6],
    'C': [7, 8, 9]
})

for c in df.columns:
    print('Column Name:', c)

Output:

Column Name: A
Column Name: B
Column Name: C

This example goes beyond inspection by demonstrating how you can iterate over the columns of a DataFrame. Such iterations can be useful for applying functions or for detailed examination of each column.

Conclusion

Inspecting the axes of a DataFrame is a fundamental skill in Pandas. Through the examples provided, we’ve seen how it aids in understanding the structure and composition of our data. Whether you’re analyzing data dimensions or customizing indexes for clearer insights, mastering these techniques will enhance your data manipulation capabilities in Pandas.