2017-05-24 96 views
1

獨特的項目我有一個pandas.core.series.Series叫名字:訪問從pandas.core.series.Series字典

print names 

0  [{'code': '8', 'name': 'John'}, {... 
1  [{'code': '1', 'name': 'Harry'},... 
2  [{'code': '5', 'name': 'Pete'... 
3  [{'code': '1', 'name': 'Harry'... 

如果只有10碼和10個唯一名稱屬於它們。我如何得到這10個名字的輸出?我的第一個猜測是:

names.unique() 

,但我得到類型錯誤:unhashable類型: '名單'

請幫助。

+0

如果我或另一種答案是有幫助的,不要忘了[接受](http://meta.stackexchange.com/a/5235/295067)它。謝謝。 – jezrael

回答

0

您可以使用concat + list comprehensionDataFrame構造和Series.unique

df = pd.concat([pd.DataFrame(x) for x in names.values.tolist()]) 
un = df['name'].unique() 
print (un) 

apply + numpy.concatenate + numpy.unique另一種解決方案:

un = np.unique(np.concatenate(names.apply(lambda x: [y['name'] for y in x]))) 
print (un) 
+0

這工作謝謝! – Leigh

0

你可以試試這個:

names.str.get(0).apply(pd.Series)['name'].unique() 

.str.get訪問列表包含在名稱系列

.apply(pd.Series)第一字典該字典轉換爲數據幀

最後,看看'名字'的內容與unique