0

This is a list of dataframes.

import pandas as pd
data=[pd.DataFrame([1,2,3],columns=['a']),pd.DataFrame([]),pd.DataFrame([]),
pd.DataFrame([3,4,5,6,7],columns=['a'])]

I am trying to delete the empty dataframes from the above list that contains dataframes.

Here is what I have tried:

for i in data:
    del i.empty()
data   

which gives:

File "<ipython-input-33-d07b32efe793>", line 2
    del i.empty()
        ^ SyntaxError: cannot delete function call

Important:It needs to store them in the data variable as well

user14305909
  • 25
  • 1
  • 6

3 Answers3

2

try this:

import pandas as pd

data = [pd.DataFrame([1, 2, 3], columns=['a']), pd.DataFrame([]),
        pd.DataFrame([]),
        pd.DataFrame([3, 4, 5, 6, 7], columns=['a'])]

for i in range(len(data)-1, 0, -1):
    if data[i].empty:
        del data[i]

print(data)

The problem with your code is that df.empty returns True or False, While what you want to do is delete the item i if i.empty() returned True.

Please noted that in the range we use a reversed range in order to avoid getting list item out of range error.

snatchysquid
  • 1,283
  • 9
  • 24
1

We ca use filter

data = list(filter(lambda df: not df.empty, data))

or list comprehension

data = [df for df in data if not df.empty]

print(data)

[   a
0  1
1  2
2  3,    a
0  3
1  4
2  5
3  6
4  7]
ansev
  • 30,322
  • 5
  • 17
  • 31
-1

You can do this:

[i for i in data if len(i)>0]

Output:

[   a
0  1
1  2
2  3,    a
0  3
1  4
2  5
3  6
4  7]
IoaTzimas
  • 10,538
  • 2
  • 13
  • 30
  • The correct way is `DataFrame.empty` https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.empty.html – ansev Sep 26 '20 at 11:38