Pandas info()

The info() method in Pandas provides a concise summary of a DataFrame. This summary includes information about the index data type and column data types, non-null values, and memory usage.

Example

import pandas as pd

# sample DataFrame
data = {'A': [1, 2, None],
        'B': ['X', 'Y', 'Z']}
df = pd.DataFrame(data)

# get info
df_info = df.info()

'''
Output

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3 entries, 0 to 2
Data columns (total 2 columns):
 #   Column  Non-Null Count  Dtype  
---  ------  --------------  -----  
 0   A       2 non-null      float64
 1   B       3 non-null      object 
dtypes: float64(1), object(1)
memory usage: 180.0+ bytes
'''

info() Syntax

The syntax of the info() method in Pandas is:

df.info(verbose=None, buf=None, max_cols=None, memory_usage=None, show_counts=None)

info() Arguments

The info() method has the following arguments:

  • verbose (optional): whether to print the full summary
  • buf (optional): the buffer to write to
  • max_cols (optional): specifies when to switch from the verbose to the truncated output
  • memory_usage (optional): whether to include the memory usage
  • show_counts (optional): whether to show the non-null counts.

info() Return Value

The info() method does not return a value; instead, it prints the summary to the console or a specified buffer.


Example 1: Complete Information

import pandas as pd

data = {'A': [1, 2, 3],
        'B': ['X', 'Y', 'Z']}
df = pd.DataFrame(data)

# get complete info
df.info()

Output

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3 entries, 0 to 2
Data columns (total 2 columns):
 #   Column  Non-Null Count  Dtype 
---  ------  --------------  ----- 
 0   A       3 non-null      int64 
 1   B       3 non-null      object
dtypes: int64(1), object(1)
memory usage: 180.0+ bytes

In this example, we printed the complete information about the DataFrame.


Example 2: Concise Summary

import pandas as pd

data = {'A': [1, 2, 3],
        'B': ['X', 'Y', 'Z']}
df = pd.DataFrame(data)

# get concise summary
df.info(verbose=False)

Output

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3 entries, 0 to 2
Columns: 2 entries, A to B
dtypes: int64(1), object(1)
memory usage: 180.0+ bytes

In this example, we printed a less detailed summary using verbose=False. The verbose argument specifies whether to give a full summary or not.

A full summary would look something like this:

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3 entries, 0 to 2
Data columns (total 2 columns):
 #   Column  Non-Null Count  Dtype 
---  ------  --------------  ----- 
 0   A       3 non-null      int64 
 1   B       3 non-null      object
dtypes: int64(1), object(1)
memory usage: 180.0+ bytes

Setting verbose=False makes the output more concise and less detailed.


Example 3: Memory Usage and Not-Null Count

import pandas as pd

data = {'A': [1, 2, None],
        'B': ['X', 'Y', 'Z']}
df = pd.DataFrame(data)

# get basic info
df.info(memory_usage='deep', show_counts=False)

Output

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3 entries, 0 to 2
Data columns (total 2 columns):
# Column Dtype --- ------ ----- 0 A float64 1 B object
dtypes: float64(1), object(1)
memory usage: 330.0 bytes

In this example, we got detailed information about memory usage using memory_usage='deep'.

We also hid the count of not-null elements using show_counts=False.


Example 4: Max Cols

import pandas as pd

data = {'A': [1, 2, None],
        'B': ['X', 'Y', 'Z'],
        'C': [4, 5, 6]}
df = pd.DataFrame(data)

# get concise summary
# if number of columns exceeds 2
print("max_cols=2")
df.info(max_cols=2)
print()

# if number of columns exceeds 3
print("max_cols=3")
df.info(max_cols=3)

Output

max_cols=2
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3 entries, 0 to 2
Columns: 3 entries, A to C
dtypes: float64(1), int64(1), object(1)
memory usage: 204.0+ bytes

max_cols=3
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3 entries, 0 to 2
Data columns (total 3 columns):
 #   Column  Non-Null Count  Dtype  
---  ------  --------------  -----  
 0   A       2 non-null      float64
 1   B       3 non-null      object 
 2   C       3 non-null      int64  
dtypes: float64(1), int64(1), object(1)
memory usage: 204.0+ bytes

Here, the number of columns in df is 3. If the number of columns exceeds the max_cols value, a concise summary is provided instead of a detailed description.

When,

  • max_cols=2, the limit is exceeded so only a concise summary is printed
  • max_cols=3, the number of columns is within the limits so a detailed description is printed

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges