Sub topic 4
Reading the output of info
This chapter assumes you know basic Python syntax, have pandas installed and imported as pd, and know that a DataFrame is a table of rows and labelled columns. Earlier chapters covered head, tail, shape, and dtypes. info combines several of those facts in one report.
What info does
DataFrame.info() prints a technical summary of the table to the console. It reports the index type and range, each column's non-null count and dtype, a tally of dtypes, and memory use. It returns None, so wrapping it in print only adds a stray None to your output.
Here is the DataFrame used throughout this tutorial:
import numpy as np
import pandas as pd
df = pd.DataFrame({
"name": ["Ana", "Ben", "Cleo", "Dev", "Eli"],
"age": [34, 28, np.nan, 45, 39],
"city": ["Lima", "Oslo", "Lima", "Pune", "Oslo"],
"salary": [52000, 48000, 61000, 75000, 58000],
})
df.info()The output looks like this. The memory figure varies by pandas version, so it is left out of the sample:
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 5 entries, 0 to 4
Data columns (total 4 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 name 5 non-null object
1 age 4 non-null float64
2 city 5 non-null object
3 salary 5 non-null int64
dtypes: float64(1), int64(1), object(2)
memory usage: ...Reading it from top to bottom
Read the report in the order it prints.
- Class line.
pandas.core.frame.DataFrameconfirms you have a DataFrame and not a Series. - Index line.
RangeIndex: 5 entries, 0 to 4means the row labels are the default integers 0 to 4, and there are 5 rows. A different index type, such asDatetimeIndex, appears here too. - Column table. Each row shows the column's position, name, non-null count, and dtype.
- Dtype tally.
dtypes: float64(1), int64(1), object(2)counts the columns of each type. - Memory line. This is the approximate size of the DataFrame in memory.
Using non-null counts to find missing values
The Non-Null Count column states how many values are present, not how many are missing. Compare it with the entry count from the index line. In the sample, age has 4 non-null values out of 5 entries, so one age is missing. All other columns are complete.
This also explains why age is float64 and not int64. NumPy integers cannot hold NaN, so pandas converts a numeric column with a missing value to floats. A float dtype on a column you expected to be whole numbers often means missing data.
To cross-check, count the missing values directly:
print(df.isna().sum())For age, the result is 1, which matches the gap in the info report.
Memory usage and the deep option
The memory figure that info shows by default is a quick estimate. For object columns, which hold Python objects such as strings, it counts only the pointers and not the strings themselves. That is why the line may end in a +, as in 292.0+ bytes. The plus sign means the real figure is at least that large.
To measure the full cost, pass memory_usage="deep":
df.info(memory_usage="deep")Deep mode inspects every object, so it is slower on large tables. It gives the number you need when deciding whether a DataFrame will fit in memory.
Common errors
- Printing the result.
print(df.info())shows the report and thenNone. Calldf.info()alone. - Reading the count as missing values. A count of 4 means 4 present values, not 4 missing.
- Trusting
objectas "text". Anobjectcolumn can hold strings, mixed types, or numbers stored as text. If a column such assalaryshowsobject, its values were probably read as strings and need conversion. - Truncated output on wide tables. With very many columns,
infomay drop the per-column table. Passverbose=Trueto force the full table. - Version differences. Newer pandas versions may display text columns with a
strdtype instead ofobject. The reading steps stay the same.
Exercise
A DataFrame has 1,000 entries. info shows the column score with a non-null count of 940 and dtype float64. How many values are missing, and why might the column be a float even if scores are whole numbers?
Show answer
60 values are missing (1,000 minus 940). The column is likely float64 because NumPy integer dtypes cannot store NaN, so pandas stores a numeric column that contains missing values as floats.
Exercise
You run print(df.info(memory_usage="deep")). What do you see in addition to the report, and why?
Show answer
You also see None printed after the report. info writes its report to the console itself and returns None, so print displays that return value.