Pandas dataframe.info() – Explained with examples

Python’s Pandas library is a cornerstone tool for handling and analyzing data. One of the fundamental components of Pandas is the DataFrame, a two-dimensional, size-mutable, and heterogeneous tabular data structure with labeled axes (rows and columns). When working with large datasets, it’s crucial to get a quick overview of the data’s structure and contents. This is where the dataframe.info() method comes in handy.

What is dataframe.info()?

The dataframe.info() method provides a concise summary of a DataFrame. This summary includes information about the DataFrame’s index dtype and columns, non-null values, and memory usage. It’s a powerful tool for understanding the essential characteristics of your dataset without delving into the data itself.

Why use dataframe.info()?

Before diving into data analysis or preprocessing, it’s important to understand the structure of your DataFrame. The dataframe.info() method helps you:

  1. Check the Data Types: Knowing the data types of each column is crucial for data cleaning and analysis. For instance, numerical operations can’t be performed on string columns.
  2. Identify Missing Values: The summary shows the number of non-null values in each column, helping you quickly identify columns with missing data.
  3. Understand Memory Usage: For large datasets, understanding memory usage can be critical for performance tuning and optimization.
How to Use dataframe.info()

Let’s dive into the syntax and output of the dataframe.info() method using an example.

Python
import pandas as pd

# Creating a sample DataFrame
data = {
    'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Eva'],
    'Age': [24, 27, 22, 32, 29],
    'City': ['New York', 'Los Angeles', 'Chicago', 'Houston', 'Phoenix'],
    'Score': [85.5, 92.3, 88.7, 79.9, None]
}

df = pd.DataFrame(data)

# Using the dataframe.info() method
df.info()
Output Explanation

The output of df.info() for the above DataFrame would look like this:

Bash
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 5 entries, 0 to 4
Data columns (total 4 columns):
 #   Column  Non-Null Count  Dtype  
---  ------  --------------  -----  
 0   Name    5 non-null      object 
 1   Age     5 non-null      int64  
 2   City    5 non-null      object 
 3   Score   4 non-null      float64
dtypes: float64(1), int64(1), object(2)
memory usage: 288.0+ bytes
Breakdown of the Output
  1. Class Information: The first line indicates that the object is a DataFrame.
  2. Index Range: It shows the index range of the DataFrame entries (0 to 4 in this case).
  3. Column Details: Each column’s name, the number of non-null entries, and the data type (Dtype) are listed.
  • Name: 5 non-null entries, dtype object (string).
  • Age: 5 non-null entries, dtype int64.
  • City: 5 non-null entries, dtype object (string).
  • Score: 4 non-null entries, dtype float64.
  1. Dtypes Summary: A summary of the data types present in the DataFrame (1 float64, 1 int64, 2 object).
  2. Memory Usage: The memory usage of the DataFrame (288.0+ bytes).
Customizing Memory Usage Display

You can customize the memory usage display using the memory_usage parameter. Setting it to 'deep' provides a more accurate estimate by introspecting the object data types.

Python
df.info(memory_usage='deep')
Practical Use Cases
  1. Initial Data Inspection: Use dataframe.info() to get a quick overview of the dataset when loading it for the first time.
  2. Data Cleaning: Identify columns with missing values that need to be handled.
  3. Performance Optimization: Monitor memory usage to optimize the DataFrame’s performance, especially with large datasets.

Conclusion

The dataframe.info() method is an indispensable tool in a data scientist’s toolkit. It offers a quick and informative summary of a DataFrame’s structure, helping you make informed decisions about data cleaning, analysis, and optimization. Whether you’re working with small datasets or handling big data, dataframe.info() provides the insights you need to start your analysis on the right foot.

By understanding and utilizing the dataframe.info() method, you can gain a clear and concise overview of your data, paving the way for effective data manipulation and analysis. Happy data wrangling!

Also Explore:

Leave a Comment