0

I am extracting data from a web site and I want to store it in a database. To do that I want to create and delete data frames inside a for-loop.

I tried creating a list of data frames and using a loop for deleting each element but it didn't work.

tables = [df1,df2,df3]

for table in tables:
    del table

It does not delete the data frames and does not show any error.

I can delete them one by one using 'del' but I need a more scalable solution.

del df1
del df2

3 Answers3

3

What you could do is index tables and delete a dataframe using a specific index. Note that since you're removing elements from the list while iterating, in order to avoid skipping elements start removing from the end:

for ix in reversed(range(len(tables))):
    # do something with tables[ix]
    del tables[ix]
yatu
  • 75,195
  • 11
  • 47
  • 89
0

Because when deleting while iterating through the list, you are actually only deleting a reference.

if you want to delete, try to access using index:

l = len(tables)
for i in range(l):
    del tables[0]

or just pop:

while tables:
    tables.pop(0)
aprilangel
  • 359
  • 2
  • 7
  • I tried both of your suggestions, they delete the elements inside the tables by index but not the actual data frames. After running your code, if I type 'df1' I still see the data frame – Maria Ines Aran Nov 14 '19 at 19:58
  • yes this will unlink your list with the dataframe but if you first use df1 = pd.DataFrame then the ref count for the table is still not zero. So my understanding here is that you want to release the memory? – aprilangel Nov 14 '19 at 22:59
0

del can take multiple arguments by the way:

del df1, df2, df3

but if you want to scale it maybe you can use dir() to see the variables in the scope and delete the ones you don't need

for var in dir():
      if var in tables:
          del var
msabri
  • 46
  • 4
  • Thanks for contributing. I tried it but it doesn't work as the elements inside 'tables' are data frames and not 'df1','df2' etc so it doesn't match with the variables names in dir() – Maria Ines Aran Nov 14 '19 at 19:57