2016-12-07 46 views

回答

4

沿着這些線?

import pandas as pd 
df = pd.DataFrame({'x' : [5,6,8], 'animal' : [['dog', 'cat'], ['elephant'], ['dog']]}) 
x = sum(df.animal, []) 
#x 
#Out[15]: ['dog', 'cat', 'elephant', 'dog'] 

from collections import Counter 
c = Counter(x) 
c.most_common(1) 
#Out[17]: [('dog', 2)] 
+0

它達到目的? @Alex Zaitsev –

+0

是的,它的工作原理,謝謝! –

0

也許退後一步,重新定義您的數據結構?如果你的數據框是「平坦的」,熊貓更適合。

相反的:

x animal 
0 5 [dog, cat] 
1 6 [dog] 
2 8 [elephant] 

務必:

x animal 
0 5 dog 
1 5 cat 
2 6 dog 
3 8 elephant 

現在你可以用len(df[df['animal'] == 'dog'])以及許多其他的東西大熊貓很容易算!

要彙整數據幀,參考這樣的回答: Flatten a column with value of type list while duplicating the other column's value accordingly in Pandas