0

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!

max
  • 637
  • 4
  • 12
  • 1
    Try using a list comprehension syntax. Try: `[globals()[df].memory_usage().sum() for df in dfs]`. `globals()[df]` will retrieve the value of the particular dataframe as you loop through `dfs`, and then you can easily call whatever method on it. –  Jul 26 '20 at 18:37
  • 1
    Regarding deleting multiple dataframes, see: https://stackoverflow.com/q/32247643/6276743 –  Jul 26 '20 at 18:39

1 Answers1

1

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),
 ...
Roy2012
  • 9,953
  • 2
  • 17
  • 32