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

Total bytes for each IPs in the dataset using pandas

df.value_counts(subset='DstAddr', ascending=False)

enter image description here

df.head()

enter image description here

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:

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

df['TotBytes'].sum()

But I want to find for each destination unique IP to see how much KB it is used for that IP.

>Solution :

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