sbradbio
sbradbio

Reputation: 169

pandas count unique in each row

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

Code

df['counts'] = df.location.str.strip().str.split(',').apply(len)

output

fruit   location
apples  8
mango   6
orange  5

When i try to use unique

Code

df['counts'] = df.location.str.strip().str.split(',').unique().apply(len)

Error

TypeError: unhashable type: 'list'

desired output

fruit   location
apples  4
mango   4
orange  3

Upvotes: 0

Views: 34

Answers (1)

BENY
BENY

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

Related Questions