Search code examples
python-3.xpandasboolean-operations

What is a pandas approach to counting text that matches condition?


What are some pandas approaches to being able to count rows where multiple conditions are met?

For example:

df = pd.DataFrame({ 'A' : ["1","2","3","4"],
                    'B' : pd.Timestamp('20130102'),
                    'C' : pd.Series(1,index=list(range(4)),dtype='float32'),
                    'D' : np.array([3] * 4,dtype='int32'),
                    'E' : pd.Categorical(["test","train","test","train"]),
                    'F' : 'foo' })
df

I am demonstrating the below as a way to count a single condition:

print ("Sum for 1 and 3:",(df['A']=="1").sum(),"records")

What are some ways to count both "1" and "3"?

In the above example, I would expect an output of Sum for 1 and 3: 2 records


Solution

  • in this case you can use in1d, which check appartenance :

    np.in1d(df["A"],["1","3"]).sum()
    

    This is very fast.