Sling Academy
Home/Pandas/Pandas: Inspect the Axes of a DataFrame (3 Examples)

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

Last updated: February 19, 2024

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.

Next Article: Pandas: Count the number of rows and columns in a DataFrame

Previous Article: Pandas: 3 ways to convert a DataFrame to a NumPy array

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)