与系列,列表和独特元素混淆
问题描述:
我想问一些帮助,因为我无法理解Python程序中的TypeError。 这一码:与系列,列表和独特元素混淆
users2 = np.random.choice(users,5000).tolist()
print len(users2)
print users2[0:20]
for user in users2:
tags.append(user_counters["tags"].loc[user])
print type(tags)
print set(tags)
标签的类型是列表。但是,当我申请设置()方法来采取的“标签”列表中的独特元素,出现以下错误:
TypeError: 'Series' objects are mutable, thus they cannot be hashed
好吧,我明白是什么意思,但我不明白的事情是“类型系列”。
在另一方面,如果使用:
print tags.unique()
另一个错误使得它的外观:
AttributeError: 'list' object has no attribute 'unique'
注:users_counters是数据帧的类型和用户型列出及其来自users_counters的元素。
那么,为什么类型错误的错误发生,因为标签是列表和set()是列表?
感谢您在adnvance
答
你tags
是pandas.Series
对象的列表。当您从loc
基于选择从数据帧建立您的清单:
for user in users2:
tags.append(user_counters["tags"].loc[user])
你会得到一个Series
。然后,您尝试从系列列表中删除一个列表,但不能因为系列不可排列。
那么为什么TypeError错误发生,因为标签是列表和设置()为 列表?
咦? set
接受任何迭代,并且该迭代的元素用于构造结果set
。你的迭代器是list
,元素是pandas.Series
对象。那就是问题所在。
我怀疑你有一个数据帧由一系列代表用户串的索引...
>>> df = pd.DataFrame({'tag':[1,2,3, 4], 'c':[1.4,3.9, 2.8, 6.9]}, index=['ted','sara','anne', 'ted'])
>>> df
c tag
ted 1.4 1
sara 3.9 2
anne 2.8 3
ted 6.9 4
>>>
当你做你的选择,因为你的用户索引具有非唯一数据元素,当你做以下选择,您将得到一个Series
:
>>> df['tag'].loc['ted']
user
ted 1
ted 4
Name: a, dtype: int64
>>> type(df['a'].loc['ted'])
<class 'pandas.core.series.Series'>