Python 2.7 熊猫在每一行中计数都是唯一的

Python 2.7 熊猫在每一行中计数都是唯一的,python-2.7,pandas,Python 2.7,Pandas,带2列的Df df: 我需要计算每个位置的数量,如果有多个,只把它们看作是一个< /P> 代码 输出 当我尝试使用unique时 代码 错误 期望输出 使用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 使用apply+set+len df.location.str.split('

带2列的Df

df:

我需要计算每个位置的数量,如果有多个,只把它们看作是一个< /P> 代码 输出 当我尝试使用unique时

代码 错误 期望输出
使用
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

使用
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
fruit   location
apples  8
mango   6
orange  5
df['counts'] = df.location.str.strip().str.split(',').unique().apply(len)
TypeError: unhashable type: 'list'
fruit   location
apples  4
mango   4
orange  3
df.location.str.split(',').apply(lambda x : len(set(x)))
Out[147]: 
0    4
1    4
2    4
Name: location, dtype: int64