How to check if a column contains list

Viewed 388
import pandas as pd

df = pd.DataFrame({"col1": ["a", "b", "c", ["a", "b"]]})

I have a dataframe like this, and I want to find the rows that contains list in that column. I tried value_counts() but it tooks so long and throws error at the end. Here is the error:

TypeError                                 Traceback (most recent call last)
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.map_locations()

TypeError: unhashable type: 'list'
Exception ignored in: 'pandas._libs.index.IndexEngine._call_map_locations'
Traceback (most recent call last):
  File "pandas/_libs/hashtable_class_helper.pxi", line 1709, in pandas._libs.hashtable.PyObjectHashTable.map_locations
TypeError: unhashable type: 'list'
c         1
a         1
[a, b]    1
b         1
Name: col1, dtype: int64

For bigger dataframes this tooks forever.

Here is how the desired output look like:

col1
c       1
b       1
[a,b]   1
dtype: int64
2 Answers

Iterate on rows and check type of obj in column by this condition: type(obj) == list

import pandas as pd

df = pd.DataFrame({"col1": ["a", "b", "c", ["a", "b"]]})

for ind in df.index:
   print (type(df['col1'][ind]) == list)

And here is the result:

False
False
False
True

Lists are mutable, they cannot be compared, so you can neither count the values nor set them as index. You would need to convert to tuple or set (thanks @CameronRiddell) to be able to count:

df['col1'].apply(lambda x: tuple(x) if isinstance(x, list) else x).value_counts()

Output:

c         1
b         1
a         1
(a, b)    1
Name: col1, dtype: int64
Related