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

Python: calculate mean, std within a group and keep the old columns

I have the following table

   Player_Id    MONTH_LABEL       AMT_TC
    108          APR_2022        26393.0
    108          FEB_2022        13984.0
    108          JAN_2022        16139.0
    108          JUN_2022        6194.0
    108          MAR_2022        19920.0
    109          APR_2022        46393.0
    109          FEB_2022        23984.0
    109          JAN_2022        36139.0
    109          JUN_2022        11194.0
    109          MAR_2022        19920.0

I need to calculate a column called mean, std which calculates the value based on the Player_ID group keeping all the other columns same.

The expected output

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

   Player_Id    MONTH_LABEL       AMT_TC       Mean       STD
    108          APR_2022        26393.0      16526       456
    108          FEB_2022        13984.0      16526       456
    108          JAN_2022        16139.0      16526       456
    108          JUN_2022        6194.0       16526       456
    108          MAR_2022        19920.0      16526       456
    109          APR_2022        46393.0      27526       1211
    109          FEB_2022        23984.0      27526       1211
    109          JAN_2022        36139.0      27526       1211
    109          JUN_2022        11194.0      27526       1211
    109          MAR_2022        19920.0      27526       1211

I tried using aggregate function but the old columns go away.

prepm_Month.groupby(['Wh_Player_Id', 'User_Name', 'TIME_SEGMENT']).transform({'BET_AMT_TC': 'mean', 
                                 'BET_AMT_TC': 'std'})

>Solution :

You only need to group by the Player ID:

amt = df.groupby("Player_Id")["AMT_TC"]
df["Mean"] = amt.transform("mean")
df["STD"] = amt.transform("std")
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