krizajb
krizajb

Reputation: 1814

count columns containing value pandas

I have combination of true/false/null data in one column. I wish to count number of false, number of true but don't want it to be 0 if only null's are in the column.

Example 1.csv:

column
null
null

The count for true and false must be None.

Example 2.csv:

column
null
null
true
true
true
false
false
true

true count should be 4, false count should be 2.

Example 3.csv:

column
null
null
true
true
true
true

true count should be 4, false count should be 0.

Currently counting works, but only in second and thid (2.csv, 3.csv) case:

df_o['counta'] = (df_t['column'] == 1).resample(interval).sum().astype(int)
df_o['countb'] = (df_t['column'] == 0).resample(interval).sum().astype(int)

Oh and I am using resample.

Upvotes: 1

Views: 51

Answers (1)

BENY
BENY

Reputation: 323226

IIUC

df1.column.value_counts().reindex([True,False])
Out[113]: 
True    NaN
False   NaN
Name: column, dtype: float64

Upvotes: 1

Related Questions