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:
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