Built with Tutify.io

    AIStudyLabs

    Pandas DataFrame info() Function

    Pandas DataFrame info() Function

    In pandas, the info() function is used to quickly get a summary of the DataFrame. It provides essential details such as the number of non-null entries in each column, the data type of each column, and the memory usage of the DataFrame. This function is very helpful for understanding the structure of your data at a glance, especially when dealing with large datasets.

    In this article, I will explain the Pandas DataFrame info() function, covering its syntax, parameters, usage, and the details it provides. The info() function in pandas offers a concise summary of a DataFrame, including essential details such as index dtype, column dtypes, non-null values, and memory usage.

    Key Points –

    <a id="h-syntax-of-pandas-dataframe-info-function"></a>

    Syntax of Pandas DataFrame info() Function

    Following is the syntax of the Pandas DataFrame info().

    
    # Syntax of Pandas DataFrame info()
    DataFrame.info(verbose=None, buf=None, max_cols=None, memory_usage=None, null_counts=None)
    

    <a id="h-parameters-of-the-nbsp-dataframe-info"></a>

    Parameters of the DataFrame info()

    Following are the parameters of the DataFrame info() function.

    <a id="h-return-value"></a>

    Return Value

    The info() function in pandas does not return any value. Instead, it prints a summary of the DataFrame to the console or the specified buffer. This summary includes details such as the number of non-null entries, data types of the columns, and memory usage.

    <a id="h-usage-of-pandas-dataframe-info-function"></a>

    Usage of Pandas DataFrame info() Function

    The info() function in pandas is commonly used to gain a quick understanding of the structure and content of a DataFrame.

    To run some examples of the Pandas DataFrame info() function, let’s create a Pandas DataFrame using data from a dictionary.

    
    # Create Pandas DataFrame
    import pandas as pd
    import numpy as np
    
    technologies= {
        'Courses':["Spark", "PySpark", "Hadoop", "Python", "Pandas"],
        'Fee' :[22000, 25000, 23000, 24000, 26000],
        'Discount':[1000, 2300, 1500, 1200, 2500],
        'Duration':['35days', '50days', '40days', '30days', '25days']
              }
    
    df = pd.DataFrame(technologies)
    print("Original DataFrame:\n", df)
    

    Yields below output.

    Alternatively, to demonstrate the basic usage of the info() function with the DataFrame you've created, you can call the info() method on the DataFrame. This will provide a concise summary of the DataFrame, including the number of entries, the column names, non-null counts, and data types.

    
    # Basic usage of info() function
    print("DataFrame Info:\n")
    df.info()
    

    Here,

    <a id="h-memory-usage-deep-analysis"></a>

    Memory Usage (Deep Analysis)

    To perform a deep memory usage analysis using the info() function in pandas, you can use the memory_usage=deep parameter. This will provide a detailed breakdown of memory usage for each column, which can be especially useful for large datasets or when you need to optimize memory consumption.

    
    # Memory usage analysis with deep introspection
    print("DataFrame Info with Deep Memory Usage:\n")
    df.info(memory_usage='deep')
    
    # Output:
    # DataFrame Info with Deep Memory Usage:
    
    # 
    # RangeIndex: 5 entries, 0 to 4
    # Data columns (total 4 columns):
    #  #   Column    Non-Null Count  Dtype 
    # ---  ------    --------------  ----- 
    #  0   Courses   5 non-null      object
    #  1   Fee       5 non-null      int64 
    #  2   Discount  5 non-null      int64 
    #  3   Duration  5 non-null      object
    # dtypes: int64(2), object(2)
    # memory usage: 838.0 bytes
    

    <a id="h-using-info-function-with-verbose-option"></a>

    Using info() Function with Verbose Option

    The verbose option in the info() function controls whether to print a full summary or a truncated one. When verbose=True, the function prints detailed information about the DataFrame, including all columns. When verbose=False, the output is shorter and might omit some columns if there are many.

    When you call df.info(verbose=True), the output includes detailed information about the DataFrame, listing all columns, their data types, and memory usage.

    
    # Using info() function with verbose=True
    print("DataFrame Info with Verbose=True:\n")
    df.info(verbose=True)
    
    # Output:
    # DataFrame Info with Verbose=True:
    
    # 
    # RangeIndex: 5 entries, 0 to 4
    # Data columns (total 4 columns):
    #  #   Column    Non-Null Count  Dtype 
    # ---  ------    --------------  ----- 
    #  0   Courses   5 non-null      object
    #  1   Fee       5 non-null      int64 
    #  2   Discount  5 non-null      int64 
    #  3   Duration  5 non-null      object
    # dtypes: int64(2), object(2)
    # memory usage: 288.0+ bytes
    

    When you call df.info(verbose=False), the output might be shorter. In this example, since the DataFrame is small, there is no significant difference, but for larger DataFrames, it would truncate the output to show fewer details.

    
    # Using info() function with verbose=False
    print("DataFrame Info with Verbose=False:\n")
    df.info(verbose=False)
    
    # Output:
    # DataFrame Info with Verbose=False:
    
    # 
    # RangeIndex: 5 entries, 0 to 4
    # Columns: 4 entries, Courses to Duration
    # dtypes: int64(2), object(2)
    # memory usage: 288.0+ bytes
    

    <a id="h-using-info-function-to-missing-values"></a>

    Using info() Function to Missing Values

    Similarly, using the info() function is particularly helpful when dealing with DataFrames that contain missing values. It helps you quickly identify which columns have missing data and how many entries are missing.

    
    # Using the info() function to identify missing values
    print("DataFrame info with missing values:\n")
    df.info()
    
    # Output:
    # DataFrame info with missing values:
    
    # 
    # RangeIndex: 5 entries, 0 to 4
    # Data columns (total 4 columns):
    #  #   Column    Non-Null Count  Dtype  
    # ---  ------    --------------  -----  
    #  0   Courses   4 non-null      object 
    #  1   Fee       4 non-null      float64
    #  2   Discount  4 non-null      float64
    #  3   Duration  4 non-null      object 
    # dtypes: float64(2), object(2)
    # memory usage: 288.0+ bytes
    

    <a id="h-faq-on-nbsp-pandas-dataframe-info-function"></a>

    FAQ on Pandas DataFrame info() Function

    The info() function provides a concise summary of a DataFrame, including the number of entries, column names, non-null counts, data types, and memory usage.

    The memory_usage parameter, when set to True or 'deep', includes memory usage information in the summary. Setting it to 'deep' provides a detailed memory usage analysis.

    The verbose parameter controls the verbosity of the output. When set to True, it provides a full summary of the DataFrame. When set to False, it provides a truncated summary.

    The info() function shows the number of non-null (non-missing) entries for each column, which helps in identifying columns with missing values.

    The max_cols parameter limits the number of columns to be printed. If not specified, it defaults to pandas' global settings.

    <a id="h-conclusion"></a>

    Conclusion

    In this article, you have explored the Pandas DataFrame info() function, including its syntax, parameters, and usage. The info() method does not return a value; instead, it prints a summary to the console or a specified buffer.

    Happy Learning!!

    <a id="h-related-articles"></a>

    Related Articles

    <a id="h-reference"></a>

    Reference