Checking memory usage of DataFrame in Pandas
Start your free 7-days trial now!
To check the memory usage of a DataFrame in Pandas we can use the info(~)
method or memory_usage(~)
method. The info(~)
method shows the memory usage of the whole DataFrame, while the memory_usage(~)
method shows memory usage by each column of the DataFrame.
Examples
Consider the following DataFrame:
df = pd.DataFrame({"A":[4,5,6],"B":["K","KK","KKK"], "C": [True,False,True], "D":[4.0,5.0,6.0]}, index=[10,11,12])df
A B C D10 4 K True 4.011 5 KK False 5.012 6 KKK True 6.0
info method
To check the total memory usage of the DataFrame:
<class 'pandas.core.frame.DataFrame'>Int64Index: 3 entries, 10 to 12Data columns (total 4 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 A 3 non-null int64 1 B 3 non-null object 2 C 3 non-null bool 3 D 3 non-null float64dtypes: bool(1), float64(1), int64(1), object(1)memory usage: 252.0 bytes
We can see at the end of the output that the memory used by this DataFrame is 252 bytes. By passing memory_usage="deep"
we are ensuring we calculate the actual memory usage rather than an estimate.
memory_usage method
To check the memory usage by each column of the DataFrame:
Index 24A 24B 177C 3D 24dtype: int64
Note the specifying deep=True
ensures we calculate the actual memory usage rather than an estimate.
By summing the memory usage of the columns and Index, we get the memory usage of the whole DataFrame:
252
Note that this matches with the memory usage returned by the info(~)
method.