计算每个值在pandas列中出现的次数

时间:2019-01-23 12:37:53

标签: python pandas

import pandas as pd 

test_values = []

test_values.append(np.array([1,0,1]))
test_values.append(np.array([1,0,1]))
test_values.append(np.array([0,1,1]))

test_values

df = pd.DataFrame(test_values)

渲染此数据框将产生:

   0  1  2
0  1  0  1
1  1  0  1
2  0  1  1

我正在尝试计算每个值在列中出现的次数,因此对于上面的数据框,应生成以下内容:

1 occurs 2, 0 occurs 0. 
0 occurs 2, 1 occurs 1. 
1 occurs 3, 0 occurs 0.

使用.values():

for i in range(0 , df.shape[1]) : 
    print(df.iloc[:,i].value_counts().values)

产生:

[2 1]
[2 1]
[3]

标签已从每列中删除。如何为每个计数访问关联的标签?这样就可以产生:

1 occurs 2, 0 occurs 0. 
0 occurs 2, 1 occurs 1. 
1 occurs 3, 0 occurs 0.

3 个答案:

答案 0 :(得分:2)

如果只有预期的01值,请添加reindex以添加缺失的值-按预期值的列表重新编制索引:

for i in range(0 , df.shape[1]) : 
    a = df.iloc[:,i].value_counts().reindex([0,1], fill_value=0)
    print (', '.join('{} occurs {}.'.format(k, v) for k, v in a.items()))

0 occurs 1., 1 occurs 2.
0 occurs 2., 1 occurs 1.
0 occurs 0., 1 occurs 3.

答案 1 :(得分:2)

您可以通过pd.Series.items迭代一个系列:

for i in range(0 , df.shape[1]):
    counts = df.iloc[:,i].value_counts()
    gen = (f'{key} occurs {value} times' for key, value in counts.items())
    print(*gen, sep=', ')

目前尚不清楚您期望如何推断零计数,因此我没有假设这是必要条件。结果给出:

1 occurs 2 times, 0 occurs 1 times
0 occurs 2 times, 1 occurs 1 times
1 occurs 3 times

答案 2 :(得分:2)

简单的解决方案:

df.apply(pd.Series.value_counts)