简体   繁体   中英

How to count number of rows in a group that have an exact string match in pandas?

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.

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

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)

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