Pandas groupby and where to new DF

Question:

Action   Symbol         Price      
   E       aaa          164  
   B       aaa          167  
   S       aaa          168  
   E       yyy          173  
   S       yyy          176  
   B       yyy          178  
   E       yyy          175  

I have a df as follows, where for each symbol there are corresponding actions. I’m attempting to count Actions equal to ‘E’, for each unique symbol, and map it to the symbols that I have stored in another dataframe (df2).

My expected output is as follows (df2)

Symbol       CountE   
aaa          1 
yyy          2  

I’m attempting to use mapping/groupby to accomplish this, but I dont know how to add a conditional to where it only will sum Actions == ‘E’. How can I go about doing so?

df2['CountE'] = df2['Symbol'].map(df.groupby(by='Symbol'['Action']=='E'.count())
Asked By: skepticalforever

||

Answers:

Use groupby.sum on a boolean Series:

df['Action'].eq('E').groupby(df['Symbol']).sum()

Or with value_counts:

df.loc[df['Action'].eq('E'), 'Symbol'].value_counts()

Output (as Series):

Symbol
aaa    1
yyy    2
Name: Action, dtype: int64

To map in another DataFrame df2 using an existing column:

df2['CountE'] = df2['Symbol'].map(df['Action'].eq('E').groupby(df['Symbol']).sum())
Answered By: mozway
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.