Pandas DataFrame.info
Pandas DataFrame.info
The DataFrame.info
method in pandas provides essential details about a DataFrame, such as the index type, column data types, non-null values, and memory usage. It is a convenient way to get a quick overview of the structure and contents of a DataFrame.
Syntax
The syntax for the DataFrame.info
method is:
DataFrame.info(verbose=None, buf=None, max_cols=None, memory_usage=None, show_counts=None)
Here, DataFrame
refers to the pandas DataFrame whose information is being displayed.
Parameters
Parameter | Description |
---|---|
verbose | Controls whether to show a summary of the DataFrame. If None , it depends on the number of columns. |
buf | File-like object where the output is written. Defaults to sys.stdout . |
max_cols | Limits the number of columns to display. If None , it defaults to pandas.options.display.max_info_columns . |
memory_usage | Displays memory usage. Can be True , False , or 'deep' for detailed memory usage. |
show_counts | Displays the count of non-null values per column if True . Defaults to None , depending on verbose . |
Examples
Basic Usage
To display basic information about a DataFrame, use the DataFrame.info
method without any parameters.
Python Program
import pandas as pd
# Create a DataFrame
data = {
'Name': ['Arjun', 'Ram', 'Suresh'],
'Age': [25, 30, 35],
'Salary': [70000, 80000, 90000]
}
df = pd.DataFrame(data)
# Display DataFrame information
print("Basic Information about the DataFrame:")
df.info()
Output
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3 entries, 0 to 2
Data columns (total 3 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Name 3 non-null object
1 Age 3 non-null int64
2 Salary 3 non-null int64
dtypes: int64(2), object(1)
memory usage: 200.0 bytes
Using memory_usage='deep'
Display detailed memory usage, including object types.
Python Program
import pandas as pd
# Create a DataFrame
data = {
'Name': ['Arjun', 'Ram', 'Suresh'],
'Age': [25, 30, 35],
'Salary': [70000, 80000, 90000]
}
df = pd.DataFrame(data)
# Display detailed memory usage
print("Detailed Memory Usage:")
df.info(memory_usage='deep')
Output
Detailed Memory Usage:
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3 entries, 0 to 2
Data columns (total 3 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Name 3 non-null object
1 Age 3 non-null int64
2 Salary 3 non-null int64
dtypes: int64(2), object(1)
memory usage: 544.0 bytes
Using show_counts=True
Enable non-null value counts for all columns.
Python Program
import pandas as pd
# Create a DataFrame
data = {
'Name': ['Arjun', 'Ram', 'Suresh'],
'Age': [25, 30, 35],
'Salary': [70000, 80000, 90000]
}
df = pd.DataFrame(data)
# Display non-null value counts
df.info(show_counts=True)
Output
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3 entries, 0 to 2
Data columns (total 3 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Name 3 non-null object
1 Age 3 non-null int64
2 Salary 3 non-null int64
dtypes: int64(2), object(1)
memory usage: 200.0 bytes
Summary
In this tutorial, we explored the DataFrame.info
method in pandas. Key takeaways include:
- Inspecting DataFrame structure and column types
- Displaying memory usage and non-null counts
- Customizing verbosity and output destination
The DataFrame.info
method is a powerful tool for data analysis and understanding dataset structure.