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
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") .
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.
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
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