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

how do you add values in pandas dataframe column based on group values

I have a data frame like this

print(df)

    Hostname  Slot Port Reserved
    Server1  1    1   0
    Server1  1    2   0
    Server1  2    3   1
    Server2  2    1   1
    Server2  2    2   0
    Server2  2    3   1
    Server3  1    1   0
    Server3  2    2   0
    Server3  3    3   1

I need to sum Reserved column by Hostname and Slot columns.

  Hostname  Slot  Total_Reserved
    Server1  1    1   
    Server2  2    2  
    Server3  1    1  

I tried this to no avail:

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

new_df = df.groupby([Hostname', 'Slot', 'Reserved']).sum()

>Solution :

groupby.sum is the way to go, but your current command is incorrect.

You need to remove "Reserved" from the groups:

out = (df
       .query('Reserved > 0')
       .groupby(['Hostname', 'Slot'], as_index=False)
       ['Reserved'].sum()
       )

Output:

  Hostname  Slot  Reserved
0  Server1     2         1
1  Server2     2         2
2  Server3     3         1

Alternatively:

out = (df
       .groupby(['Hostname', 'Slot'])
       [['Reserved']].sum()
       .add_prefix('Total_').reset_index()
       )

Output:

  Hostname  Slot  Total_Reserved
0  Server1     1               0
1  Server1     2               1
2  Server2     2               2
3  Server3     1               0
4  Server3     2               0
5  Server3     3               1
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