xbe.at

Data science

Pandas DataFrame Attributes and Python Code Examples

4 min read, 14 sections

Contents
  1. Pandas DataFrame Attributes
  2. DataFrame.shape
  3. DataFrame.dtypes
  4. DataFrame.index
  5. DataFrame.columns
  6. DataFrame.values
  7. DataFrame.empty
  8. DataFrame.size
  9. DataFrame.ndim
  10. DataFrame.axes
  11. DataFrame.info()
  12. Real-life Example: Weather Data Analysis
  13. Real-life Example: Student Performance Analysis
  14. Additional Resources

Pandas DataFrame Attributes

DataFrames are the most commonly used data structure in pandas. They are two-dimensional labeled data structures with columns of potentially different types. Understanding DataFrame attributes is crucial for effective data manipulation and analysis.

import pandas as pd

# Create a sample DataFrame
df = pd.DataFrame({
    'Name': ['Alice', 'Bob', 'Charlie'],
    'Age': [25, 30, 35],
    'City': ['New York', 'London', 'Paris']
})

print(df)

DataFrame.shape

The shape attribute returns a tuple representing the dimensionality of the DataFrame. It provides the number of rows and columns in the DataFrame.

# Get the shape of the DataFrame
shape = df.shape

print(f"Number of rows: {shape[0]}")
print(f"Number of columns: {shape[1]}")

DataFrame.dtypes

The dtypes attribute returns the data types of each column in the DataFrame. This is essential for understanding the nature of your data and performing appropriate operations.

# Display the data types of each column
print(df.dtypes)

# Change the data type of a column
df['Age'] = df['Age'].astype(float)
print(df.dtypes)

DataFrame.index

The index attribute represents the row labels of the DataFrame. It can be customized to use meaningful identifiers instead of default integer indices.

# Display the current index
print(df.index)

# Set a custom index
df.set_index('Name', inplace=True)
print(df)
print(df.index)

DataFrame.columns

The columns attribute returns the column labels of the DataFrame. It can be used to access, modify, or rename columns.

# Display column names
print(df.columns)

# Rename columns
df.columns = ['Years', 'Location']
print(df)

DataFrame.values

The values attribute returns a NumPy array containing the data in the DataFrame. This is useful when you need to perform operations that require a pure NumPy array.

# Get the values as a NumPy array
array_data = df.values
print(array_data)
print(type(array_data))

DataFrame.empty

The empty attribute returns a boolean indicating whether the DataFrame is empty (contains no data). This is useful for error checking and flow control in data processing pipelines.

# Check if the DataFrame is empty
print(f"Is the DataFrame empty? {df.empty}")

# Create an empty DataFrame
empty_df = pd.DataFrame()
print(f"Is the new DataFrame empty? {empty_df.empty}")

DataFrame.size

The size attribute returns the total number of elements in the DataFrame. It is equal to the number of rows multiplied by the number of columns.

# Get the size of the DataFrame
print(f"Total number of elements: {df.size}")

# Verify the calculation
total_elements = df.shape[0] * df.shape[1]
print(f"Calculated total elements: {total_elements}")

DataFrame.ndim

The ndim attribute returns the number of dimensions of the DataFrame. For a standard DataFrame, this will always be 2 (rows and columns).

# Get the number of dimensions
print(f"Number of dimensions: {df.ndim}")

# Create a Series (1-dimensional) for comparison
series = pd.Series([1, 2, 3])
print(f"Number of dimensions in a Series: {series.ndim}")

DataFrame.axes

The axes attribute returns a list of the row axis labels and column axis labels. This can be useful for understanding the structure of your DataFrame.

# Get the axes of the DataFrame
axes = df.axes
print(f"Row labels: {axes[0]}")
print(f"Column labels: {axes[1]}")

DataFrame.info()

While not strictly an attribute, the info() method provides a concise summary of the DataFrame, including the index dtype and column dtypes, non-null values, and memory usage.

# Display DataFrame info
df.info()

# Display DataFrame info with memory usage
df.info(memory_usage="deep")

Real-life Example: Weather Data Analysis

Let's use DataFrame attributes to analyze weather data for different cities.

import pandas as pd
import numpy as np

# Create a DataFrame with weather data
weather_data = pd.DataFrame({
    'City': ['Tokyo', 'New York', 'London', 'Paris'],
    'Temperature': [25.5, 22.1, 18.7, 20.3],
    'Humidity': [60, 55, 70, 65],
    'Wind_Speed': [10.2, 8.5, 12.1, 9.8]
})

print(weather_data)
print(f"\nShape: {weather_data.shape}")
print(f"\nData Types:\n{weather_data.dtypes}")
print(f"\nColumn Names: {weather_data.columns}")

Real-life Example: Student Performance Analysis

Let's use DataFrame attributes to analyze student performance data.

# Create a DataFrame with student performance data
student_data = pd.DataFrame({
    'Student_ID': ['S001', 'S002', 'S003', 'S004', 'S005'],
    'Math_Score': [85, 92, 78, 95, 88],
    'Science_Score': [90, 88, 82, 96, 85],
    'Literature_Score': [75, 85, 92, 88, 91]
})

student_data.set_index('Student_ID', inplace=True)
print(student_data)
print(f"\nIndex: {student_data.index}")
print(f"\nSize: {student_data.size}")
print(f"\nMean Scores:\n{student_data.mean()}")

Additional Resources

For more advanced topics and in-depth explanations of pandas DataFrame attributes, consider exploring the following resources:

  1. Official pandas documentation: https://pandas.pydata.org/docs/
  2. "Effective Pandas" by Matt Harrison: https://github.com/mattharrison/effective_pandas
  3. "Python for Data Analysis" by Wes McKinney (creator of pandas): https://wesmckinney.com/book/

These resources provide comprehensive coverage of pandas and its capabilities, helping you master DataFrame manipulation and analysis.

More in Data science

  1. Pandas DataFrame Memory Layout and Efficient Iteration8 min

    The fundamental structure of Pandas DataFrame follows a column-major order where data is stored contiguously in memory by columns rather than rows. This...

  2. Optimizing NumPy Performance with Thread Configuration6 min

    NumPy's performance heavily depends on its underlying BLAS (Basic Linear Algebra Subprograms) implementation and thread configuration. The three main...

  3. Pandas for Data Wrangling and Analysis6 min

    Pandas is a powerful Python library for data manipulation and analysis. It provides data structures like DataFrame and Series, which allow for efficient...

  4. Optimizing Data Type Conversions in Pandas with astype()10 min

    Data type conversion is a critical operation in data preprocessing that impacts memory usage and computational efficiency. The astype() method in Pandas...

  5. Pandas Inplace Operations Expectations vs. Reality6 min

    The inplace parameter in Pandas operations is commonly misunderstood. While developers expect it to modify data structures directly without creating copies...

  6. NumPy Shortcuts for Efficient Data Analysis6 min

    NumPy is a fundamental library for scientific computing in Python. This guide will walk you through essential NumPy commands and operations, helping you...