Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a column contains list

Tags:

python

pandas

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
like image 321
erentknn Avatar asked Nov 05 '20 18:11

erentknn


People also ask

How do you check if a column contains a substring?

Using “contains” to Find a Substring in a Pandas DataFrame The contains method returns boolean values for the Series with True for if the original Series value contains the substring and False if not. A basic application of contains should look like Series. str. contains("substring") .

How do I get a list of columns in pandas?

You can get the column names from pandas DataFrame using df. columns. values , and pass this to python list() function to get it as list, once you have the data you can print it using print() statement.


1 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
like image 136
Pouya Esmaeili Avatar answered Oct 23 '22 03:10

Pouya Esmaeili