Given a DataFrame like this:
dft = pd.DataFrame([[12, 'Bob', True],
[123, 'Henry', False],
[768, 'Bob', False]],
columns=['TID', 'UID', 'TRUTH'])
Which looks like this:
TID UID TRUTH
0 12 Bob True
1 123 Henry False
3 768 Bob False
I want to aggregate over UID
and count where TRUTH
is True. So the output should look like:
UID TRUTH
0 Bob 1
1 Henry 0
I have already tried:
dft.groupby('UID').agg({'TRUTH': pd.Series.nunique}) # counts all values T and F
I am conceptually struggling to see how to put the condition together with the aggregation.
Thanks for your help!
Is this what you're looking for?
print(dft.groupby('UID')['TRUTH'].sum().astype(int))
Output:
UID
Bob 1
Henry 0
Name: TRUTH, dtype: int32
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments