max
max

Reputation: 693

Print Size of All (multiple) Pandas DataFrames

quick question - I have 20 dataframes in a Jupyter notebook, size is getting pretty big, so I'm trying to show all the dfs and sizes and then delete them. Here's where I'm at:

show all dfs:

dfs = %who_ls DataFrame
dfs

result of show all dfs:

['cap_consumed_month_avg',
 'cap_monthly_avg',
 'cap_remaining_month_avg',
 'cap_unfilled_month_avg',
 'capacity_df',
 'daily_capacity',
 'df',
 'df1',
 'df1_fill',
 'df_fill',
 'empty_filled_time_slots',
 'empty_time_slots',
 'mapping',
 'month_store_id_demand',
 'month_store_id_total',
 'part_filled_time_slots',
 'store_df',
 'time_slot_df',
 'time_slots_day',
 'weekly_capacity']

next append a command that would get the memory_usage for each df:

memlist = []
for i in dfs:
    memory = i + ".memory_usage().sum()"
    memlist.append(memory)

print(memlist)

result of the append:

['cap_consumed_month_avg.memory_usage().sum()', 'cap_monthly_avg.memory_usage().sum()',
 'cap_remaining_month_avg.memory_usage().sum()', 'cap_unfilled_month_avg.memory_usage().sum()',
 'capacity_df.memory_usage().sum()', 'daily_capacity.memory_usage().sum()', 
'df.memory_usage().sum()', 'df1.memory_usage().sum()', 'df1_fill.memory_usage().sum()', 
'df_fill.memory_usage().sum()', 'empty_filled_time_slots.memory_usage().sum()', 
'empty_time_slots.memory_usage().sum()', 'mapping.memory_usage().sum()', 
'month_store_id_demand.memory_usage().sum()', 'month_store_id_total.memory_usage().sum()', 
'part_filled_time_slots.memory_usage().sum()', 'store_df.memory_usage().sum()', 
'time_slot_df.memory_usage().sum()', 'time_slots_day.memory_usage().sum()', 
'weekly_capacity.memory_usage().sum()']

here's where I'm stuck: other than print out each eval of each item in the list, how can I print them all out together? I'm sure there's a loop styled way to do this. And then how to delete all dfs in either a loop or something easier than 20 lines of del(df).

Thanks so much!

Upvotes: 0

Views: 462

Answers (1)

Roy2012
Roy2012

Reputation: 12523

Here's a way to do that:

dfs = %who_ls DataFrame
[(df, globals()[df].memory_usage().sum()) for df in dfs]

The output, in my case, is:

[('away', 352),
 ('data', 10230688),
 ('data_today', 57392),
 ...

Upvotes: 1

Related Questions