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: IfTrue, shows full summary; ifFalse, shows only summary info.memory_usage: Shows memory usage; can beTrue,False, or'deep'for detailed memory info.null_counts: Deprecated, useshow_countsinstead 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 returnsNone. - Not noticing that missing values show as fewer non-null counts.
- Using deprecated parameters like
null_countsin 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:
| Parameter | Description | Default |
|---|---|---|
| verbose | Show full summary if True, else concise | None |
| memory_usage | Show memory usage; True, False, or 'deep' | None |
| show_counts | Show non-null counts (replaces deprecated null_counts) | None |
| buf | Output destination (default stdout) | None |
| max_cols | Max columns to display info for | None |
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.