How to check if a column contains list

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

Tags:

Python

Pandas