Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if an element is an empty list in pandas?

Tags:

python

pandas

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
like image 466
Blaszard Avatar asked Jul 05 '19 13:07

Blaszard


People also ask

How do you check if an element in a list is empty?

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.

How do you check if an element is empty in pandas?

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 .

How do you check if a position in a list is empty Python?

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.


2 Answers

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]
like image 133
jdehesa Avatar answered Oct 09 '22 01:10

jdehesa


This is probably the most efficient solution.

df[df["col"].astype(bool)]
like image 41
GZ0 Avatar answered Oct 08 '22 23:10

GZ0