df.value_counts(subset='DstAddr', ascending=False)
df.head()
I am trying to find a way to show for each unique IP: How many in the sum of total bytes does it have? For example, if I want to find all dataset:
df['TotBytes'].sum()
But I want to find for each destination unique IP to see how much KB it is used for that IP.
CodePudding user response:
You can use df.groupby("DstAddr")
to work with groups - something like
df.groupby("DstAddr")['TotBytes'].sum()
or using loop
for key, val in df.groupby("DstAddr"):
print(key, val['TotBytes'].sum() )
Minimal working example
import pandas as pd
data = {
'A': ['1.0.0.0','2.0.0.0','3.0.0.0', '2.0.0.0',],
'B': [4,5,6,7],
'C': [7,8,9,0]
}
df = pd.DataFrame(data)
print(df)
print(df.groupby('A')['B'].sum())
for key, val in df.groupby('A'):
print('---', key, '---')
print(val['B'].sum())
Result:
A B C
0 1.0.0.0 4 7
1 2.0.0.0 5 8
2 3.0.0.0 6 9
3 2.0.0.0 7 0
A
1.0.0.0 4
2.0.0.0 12
3.0.0.0 6
Name: B, dtype: int64
--- 1.0.0.0 ---
4
--- 2.0.0.0 ---
12
--- 3.0.0.0 ---
6