简体   繁体   中英

Delete empty dataframes from a list with dataframes

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

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.

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]

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM