Reputation: 1037
I have a dataframe where I want to group by some column, and then count the number of rows that have some exact string match for some other column. Assume all dtypes are 'object'.
In pseudo-code I'm looking for something like:
df.groupby('A').filter(x['B'] == '0').size()
I want to group by column 'A', then count the number of rows of column 'B' that have an exact string match to the string '0'.
edit: I found an inelegant solution:
def counter(group):
i = 0
for item in group:
if item == '0':
i = i + 1
return i
df.groupby('A')['B'].agg(counter)
There must be a better way.
Upvotes: 1
Views: 1142
Reputation: 13715
I don't see much wrong with the solution you proposed in your question. If you wanted to make it a one liner you could do the following:
data = np.array(list('abcdefabc')).reshape((3, 3))
df = pd.DataFrame(data, columns=list('ABC'))
df
A B C
0 a b c
1 d e f
2 a b c
df.groupby('A').agg(lambda x:list(x).count('c'))
B C
A
a 0 2
d 0 0
This would have the advantage of giving all of the values for each column in the original dataframe
Upvotes: 1
Reputation: 1542
try creating a temp column which suggest if the value is zero or not and then make a pivot table based on this column
Hope this helps.
Let me know if it worked.
import pandas as pd
df=pd.DataFrame({'A':['one','one','one','one','one','one','one','two','two','two','two','two','two','two'],'B':[1,2,3,0,2,3,0,2,3,2,0,3,44,55]})
# create a new column if the values is ZERO or not.
df['C'] = df['B'].apply(lambda x: 'EQUALS_ZERO' if x==0 else 'NOT_EQUAL_ZERO')
# make a pivote table
# this will give you value for both =0 and !=0
x= pd.pivot_table(df,index=['A'],values='B',columns='C',aggfunc='count',fill_value=0)
print(x)
Upvotes: 0