Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pandas groupby and where to new DF

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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())
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading