Reputation: 169
Df with 2 columns
df:
fruit location
apples store,freezer,kitchen,livingroom,store,freezer,kitchen,livingroom
mango store,freezer,kitchen,livingroom,store,freezer
orange store,freezer,kitchen,freezer
I need to count the number of each location, incase there are multiple just consider them as one
df['counts'] = df.location.str.strip().str.split(',').apply(len)
fruit location
apples 8
mango 6
orange 5
When i try to use unique
df['counts'] = df.location.str.strip().str.split(',').unique().apply(len)
TypeError: unhashable type: 'list'
fruit location
apples 4
mango 4
orange 3
Upvotes: 0
Views: 34
Reputation: 323396
Using apply
+ set
+ len
df.location.str.split(',').apply(lambda x : len(set(x)))
Out[147]:
0 4
1 4
2 4
Name: location, dtype: int64
Upvotes: 2