Pandas: Display memory usage of a given DataFrame and every column of the DataFrame
Pandas: DataFrame Exercise-71 with Solution
Write a Pandas program to display memory usage of a given DataFrame and every column of the DataFrame.
Sample Solution :
Python Code :
import pandas as pd
df = pd.DataFrame({
'Name': ['Alberto Franco','Gino Mcneill','Ryan Parkes', 'Eesha Hinton', 'Syed Wharton'],
'Date_Of_Birth ': ['17/05/2002','16/02/1999','25/09/1998','11/05/2002','15/09/1997'],
'Age': [18.5, 21.2, 22.5, 22, 23]
})
print("Original DataFrame:")
print(df)
print("\nGlobal usage of memory of the DataFrame:")
print(df.info(memory_usage = "deep"))
print("\nThe usage of memory of every column of the said DataFrame:")
print(df.memory_usage(deep = True))
Sample Output:
Original DataFrame: Name Date_Of_Birth Age 0 Alberto Franco 17/05/2002 18.5 1 Gino Mcneill 16/02/1999 21.2 2 Ryan Parkes 25/09/1998 22.5 3 Eesha Hinton 11/05/2002 22.0 4 Syed Wharton 15/09/1997 23.0 Global usage of memory of the DataFrame: <class 'pandas.core.frame.DataFrame'> RangeIndex: 5 entries, 0 to 4 Data columns (total 3 columns): Name 5 non-null object Date_Of_Birth 5 non-null object Age 5 non-null float64 dtypes: float64(1), object(2) memory usage: 801.0 bytes None The usage of memory of every column of the said DataFrame: Index 80 Name 346 Date_Of_Birth 335 Age 40 dtype: int64
Python-Pandas Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Pandas program to convert continuous values of a column in a given DataFrame to categorical.
Next: Write a Pandas program to combine many given series to create a DataFrame.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
Python: Tips of the Day
Find current directory and file's directory:
To get the full path to the directory a Python file is contained in, write this in that file:
import os dir_path = os.path.dirname(os.path.realpath(__file__))
(Note that the incantation above won't work if you've already used os.chdir() to change your current working directory, since the value of the __file__ constant is relative to the current working directory and is not changed by an os.chdir() call.)
To get the current working directory use
import os cwd = os.getcwd()
Documentation references for the modules, constants and functions used above:
- The os and os.path modules.
- The __file__ constant
- os.path.realpath(path) (returns "the canonical path of the specified filename, eliminating any symbolic links encountered in the path")
- os.path.dirname(path) (returns "the directory name of pathname path")
- os.getcwd() (returns "a string representing the current working directory")
- os.chdir(path) ("change the current working directory to path")
Ref: https://bit.ly/3fy0R6m
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework