Python Forum
help how to get size of pandas dataframe into MB\GB
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
help how to get size of pandas dataframe into MB\GB
#1
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
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Alteryx QS-Passing pandas dataframe column inside SQL query where condition sanky1990 0 734 Dec-04-2023, 09:48 PM
Last Post: sanky1990
  Question on pandas.dataframe merging two colums shomikc 4 833 Jun-29-2023, 11:30 AM
Last Post: snippsat
  Pandas AttributeError: 'DataFrame' object has no attribute 'concat' Sameer33 5 5,644 Feb-17-2023, 06:01 PM
Last Post: Sameer33
  pandas dataframe into csv .... exponent issue mg24 10 1,793 Jan-20-2023, 08:15 PM
Last Post: deanhystad
  How to assign a value to pandas dataframe column rows based on a condition klllmmm 0 839 Sep-08-2022, 06:32 AM
Last Post: klllmmm
  How to retrieve records in a DataFrame (Python/Pandas) that contains leading or trail mmunozjr 3 1,756 Sep-05-2022, 11:56 AM
Last Post: Pedroski55
  "Vlookup" in pandas dataframe doug2019 3 1,860 May-09-2022, 01:35 PM
Last Post: snippsat
  Increase the speed of a python loop over a pandas dataframe mcva 0 1,317 Jan-21-2022, 06:24 PM
Last Post: mcva
  for loop in dataframe in pandas Paulman 7 2,760 Dec-02-2021, 12:15 AM
Last Post: bowlofred
  Structuring and pivoting corrupted dataframe in pandas gunner1905 2 2,243 Sep-18-2021, 01:30 PM
Last Post: gunner1905

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020