Python Forum

Full Version: grouped data frame
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi everyone,
I would like to group the dataframe using multiple keys. I used a data frame like (test_df) below:

dstIP srcIP attack_type port datetime
0 92.57.32.115 183.134.75.216 BruteForce 22 1604339526
1 92.57.32.115 112.85.42.121 SCAN 22 1604339522
2 92.57.32.115 112.85.42.121 BruteForce 22 1604339522
3 92.57.32.115 112.85.42.121 BruteForce 22 1604339521
4 92.57.32.115 76.102.78.161 BruteForce 22 1604339517

To aggregate rows with same dstIP, srcIP , attack_type and port, I grouped them with multiple keys and use maximum datatime as follows:

grouped = test_df.groupby(['srcIP', 'dstIP', 'attack_type','port']).aggregate({'datetime':'max'})

However, I do not know how to access each value in grouped for example I want to know the datatime for srcIP= 112.85.42.121 in grouped dataframe but I receive error when I use the following command:
Value_Target = grouped.loc[grouped['srcIP']==' 112.85.42.121 ']['datetime'].values
Also, I would like to print first value of dataframe in source IP column and I cannot use : grouped["srcIP"][0]
I will be thankful to know how I can solve my issues.
Thanks