Reputation: 35
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
Upvotes: 1
Views: 4044
Reputation: 30920
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]
Upvotes: 1
Reputation: 1352
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.
Upvotes: 3
Reputation: 10624
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]
Upvotes: -1