Python Forum
help how to get size of pandas dataframe into MB\GB - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: help how to get size of pandas dataframe into MB\GB (/thread-39302.html)



help how to get size of pandas dataframe into MB\GB - mg24 - Jan-28-2023

Hi Team,

I want to convert memory usage of DataFrame into MB OR GB. in a Pythonic way.

below value I want to get size 19647323

import pandas as pd
data = pd.read_csv(r'E:\data\mobile_list.csv')
df = pd.DataFrame(data)
print(df.memory_usage(deep=True).sum())
Ans====> 19647323
Thanks
mg


RE: help how to get size of pandas dataframe into MB\GB - snippsat - Jan-28-2023

If you search so is this a common task,so there is many soution out there.
The math is simple,can write it like this to eg get it MiB.
>>> b = 19647323
>>> print(f'{b} bytes is  {b / (1024 * 1024):3.2f} MiB')
19647323 bytes is 18.74 MiB
There are also package like humanize
>>> import humanize
>>> 
>>> b = 19647323
>>> humanize.naturalsize(b)
'19.6 MB'
So here get MB back and not MiB,which is more a human way and the most used way even if they mean MiB.
Can get the same bye doing this.
b = 19647323
>>> print(f'{b} bytes is in {b / (1000 * 1000):3.1f} MB')
19647323 bytes is in 19.6 MB
If look at it so is 1 kib is really not 1000 kb.
1 kiB = 1.024 kB
A reason for preferring MB over MiB is mostly that it's a real round number in our familiar base ten.