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
Photo Converting Pandas DataFrame to a table of hourly blocks Abedin 1 576 Apr-24-2025, 01:05 PM
Last Post: snippsat
  Most efficient way to roll through a pandas dataframe? sawtooth500 2 1,179 Aug-28-2024, 10:08 AM
Last Post: Alice12
  docx file to pandas dataframe/excel iitip92 1 2,756 Jun-27-2024, 05:28 AM
Last Post: Pedroski55
  Python Alteryx QS-Passing pandas dataframe column inside SQL query where condition sanky1990 0 1,406 Dec-04-2023, 09:48 PM
Last Post: sanky1990
  Question on pandas.dataframe merging two colums shomikc 4 1,998 Jun-29-2023, 11:30 AM
Last Post: snippsat
  Pandas AttributeError: 'DataFrame' object has no attribute 'concat' Sameer33 5 10,480 Feb-17-2023, 06:01 PM
Last Post: Sameer33
  pandas dataframe into csv .... exponent issue mg24 10 5,120 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 1,947 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 3,659 Sep-05-2022, 11:56 AM
Last Post: Pedroski55
  "Vlookup" in pandas dataframe doug2019 3 3,158 May-09-2022, 01:35 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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