One of the column in my df stores a list, and some of the raws have empty items in the list. For example:
[]
["X", "Y"]
[]
etc...
How can only take the raw whose list is not empty?
The following code does not work.
df[df["col"] != []] # ValueError: Lengths must match to compare
df[pd.notnull(df["col"])] # The code doesn't issue an error but the result includes an empty list
df[len(df["col"]) != 0] # KeyError: True
Checking empty list using len() Function. The len() function is used to find the number of elements in the list. So, to check if the list is empty or not using len(), we can pass the empty list to the len() function, and if we get 0, that means the list is empty.
shape() method returns the number of rows and number of columns as a tuple, you can use this to check if pandas DataFrame is empty. DataFrame. shape[0] return number of rows. If you have no rows then it gives you 0 and comparing it with 0 gives you True .
Empty lists are considered False in Python, hence the bool() function would return False if the list was passed as an argument. Other methods you can use to check if a list is empty are placing it inside an if statement, using the len() methods, or comparing it with an empty list.
You can do this:
df[df["col"].str.len() != 0]
Example:
import pandas as pd
df = pd.DataFrame({"col": [[1], [2, 3], [], [4, 5, 6], []]}, dtype=object)
print(df[df["col"].str.len() != 0])
# col
# 0 [1]
# 1 [2, 3]
# 3 [4, 5, 6]
This is probably the most efficient solution.
df[df["col"].astype(bool)]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With