0
0
Data-analysis-pythonHow-ToBeginner ยท 3 min read

How to Use info() in pandas to Inspect DataFrames

Use the info() method on a pandas DataFrame to get a concise summary of its structure, including the number of rows, columns, data types, and non-null counts. This helps you quickly understand your data's shape and missing values.
๐Ÿ“

Syntax

The info() method is called on a pandas DataFrame object. It has optional parameters to customize the output.

  • verbose: If True, shows full summary; if False, shows only summary info.
  • memory_usage: Shows memory usage; can be True, False, or 'deep' for detailed memory info.
  • null_counts: Deprecated, use show_counts instead in newer pandas versions.
python
DataFrame.info(verbose=None, buf=None, max_cols=None, memory_usage=None, show_counts=None)
๐Ÿ’ป

Example

This example shows how to create a DataFrame and use info() to see its structure, data types, and missing values.

python
import pandas as pd

data = {
    'Name': ['Alice', 'Bob', 'Charlie', None],
    'Age': [25, 30, 35, 40],
    'Salary': [50000, 60000, None, 80000]
}

df = pd.DataFrame(data)

# Use info() to get summary
print(df.info())
Output
<class 'pandas.core.frame.DataFrame'> RangeIndex: 4 entries, 0 to 3 Data columns (total 3 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 Name 3 non-null object 1 Age 4 non-null int64 2 Salary 3 non-null float64 dtypes: float64(1), int64(1), object(1) memory usage: 224.0+ bytes None
โš ๏ธ

Common Pitfalls

Some common mistakes when using info() include:

  • Expecting info() to return a value. It prints output and returns None.
  • Not noticing that missing values show as fewer non-null counts.
  • Using deprecated parameters like null_counts in newer pandas versions.
python
import pandas as pd

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

# Wrong: expecting info() to return data
result = df.info()
print('Returned:', result)

# Right: just call info() to print summary
# df.info()
Output
<class 'pandas.core.frame.DataFrame'> RangeIndex: 3 entries, 0 to 2 Data columns (total 1 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 A 2 non-null float64 dtypes: float64(1) memory usage: 152.0 bytes Returned: None
๐Ÿ“Š

Quick Reference

Summary of info() usage:

ParameterDescriptionDefault
verboseShow full summary if True, else conciseNone
memory_usageShow memory usage; True, False, or 'deep'None
show_countsShow non-null counts (replaces deprecated null_counts)None
bufOutput destination (default stdout)None
max_colsMax columns to display info forNone
โœ…

Key Takeaways

Use DataFrame.info() to quickly see data types, non-null counts, and memory usage.
info() prints output and returns None; do not assign its result expecting data.
Missing values show as lower non-null counts in the summary.
Avoid deprecated parameters like null_counts; use show_counts instead.
Customize output with parameters like verbose and memory_usage.