x animal
0 5 [dog, cat]
1 6 [dog]
2 8 [elephant]
我有這樣的數據框。我怎樣才能找到所有列表中包含的最常見的動物。熊貓:在列表中找到最頻繁的值
方法value_counts()將列表視爲一個元素,我無法使用它。
x animal
0 5 [dog, cat]
1 6 [dog]
2 8 [elephant]
我有這樣的數據框。我怎樣才能找到所有列表中包含的最常見的動物。熊貓:在列表中找到最頻繁的值
方法value_counts()將列表視爲一個元素,我無法使用它。
沿着這些線?
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)]
也許退後一步,重新定義您的數據結構?如果你的數據框是「平坦的」,熊貓更適合。
相反的:
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
它達到目的? @Alex Zaitsev –
是的,它的工作原理,謝謝! –