简体   繁体   中英

Count number of times each value occurs in pandas column

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)

rendering this dataframe produces :

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

I'm attempting to count the number of times each value occurs in column, so for above dataframe the following should be produced :

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

Using .values() :

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

produces :

[2 1]
[2 1]
[3]

The label has been removed from each column. How to access the associated label for each count ? So can produce :

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

If there are expected only 0 and 1 values add reindex for add missing values - reindex by list of expected values:

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.

You can iterate a series via 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=', ')

It's not clear how you expect to deduce zero counts, so I have not assumed this is a requirement. The result gives:

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

简单的解决方案:

df.apply(pd.Series.value_counts)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM