2017-05-26 84 views
0

我想問一些幫助,因爲我無法理解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

回答

2

tagspandas.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'>