计算数据集中的所有唯一值

时间:2019-04-16 16:19:46

标签: python pandas

我有60台计算机的数据集,每列是计算机,行是从每台PC安装的所有软件的集合。我希望能够计算每个唯一值(软件),因此我可以看到当前安装了每个软件的数量。

data = [['a','a','c'],['a','b','d'],['a','c','c']]
df = pd.DataFrame(data,columns=['col1','col2','col3'])
df


col1  col2  col3
a      a      c
a      b      d
a      c      c

I expect the following output
a 4
b 1
c 3

1 个答案:

答案 0 :(得分:3)

num1 = 20 num1_list = [] num2 = 40 num2_list = [] x = 1 y = 1 while x <= num1: if num1 % x == 0: num1_list.append(x) x += 1 while y <= num2: if num2 % y == 0: num2_list.append(y) y += 1 xy = list(set(num1_list).intersection(num2_list)) print(xy[-1]) value_counts之后

melt

df.melt().value.value_counts() Out[648]: a 4 c 3 b 1 d 1 Name: value, dtype: int64 加快

numpy.unique
相关问题