gpt4 book ai didi

python - pandas 按 3 个变量分组,但对其中 2 个变量求和

转载 作者:行者123 更新时间:2023-12-01 03:32:59 28 4
gpt4 key购买 nike

我有一个数据框:

In [59]: df.head()
Out[59]:

src-subnet dst-subnet bytes Start
0 ATL Internet 180 1477252800
1 ATL ATL 206 1477252800
2 ATL Private-10.0.0.0 17160 1477252800
3 SAT Internet 15768 1477252800
4 AWS-CloudFront ATL 1592 1477252800

对于每个 start 值,我想要在每对可能的 src-subnetdst-subnet 之间传输的字节总和。

我写了以下内容:

g_start = df.groupby('Start')
g_start_subnet = g_start.apply(lambda x: x.groupby(['src-subnet', 'dst-subnet']).sum())

我得到了:

```

In [60]: g_start_subnet
Out[60]:

bytes
Start src-subnet dst-subnet
1476896400 ATL ATL 5190647907
AWS 67442
AWS-CloudFront 3523866
AWS-EC2 10231320
Broadcast-255.255.255.255 550470
COL 25459487
Corvil-CNE 5130
DEN 68247258
DFB 49930518
DFW 109329393
HOU 43601191
IND 97444445
Internal Network 2295849
Internet 72555057
JES 64789642
LAF 30442304
LOU 27372802
MGO 307340
Multicast-224.0.0.0 25339730
NOL 49526023
Private-10.0.0.0 814280925
Private-172.16.0.0 9348122
Private-192.168.0.0 8907521
SAT 43590577
SCI 2473674
WMSApp 74085307
WMSDB 9604726
AWS ATL 78309
AWS-CloudFront ATL 10026686
HOU 9285
... ...
1477497600 Private-10.0.0.0 Internet 90581028
Link-169.254.0.0 276
Private-10.0.0.0 159103776
Private-192.168.0.0 60724723
WMSApp 1100
Private-100.64.0.0 ATL 1304731
Private-192.168.0.0 ATL 77031
Internet 926
Multicast-224.0.0.0 238220
Private-10.0.0.0 19320
Private-192.168.0.0 393982
SAT ATL 18278815
AWS-EC2 28380
Internet 42773386
Private-10.0.0.0 8030664
Private-172.16.0.0 230389
Private-192.168.0.0 5938773
SCI ATL 2055407
Internet 900
Multicast-224.0.0.0 256
Solace-Servers Broadcast-255.255.255.255 153438
WMSApp ATL 95861882
AWS-EC2 360
DFB 3066
DFW 9000
Internet 3330
Multicast-224.0.0.0 31292117
Private-10.0.0.0 1022
WMSDB ATL 8678562
Internet 2160

我如何知道删除多重索引并获得像数据帧这样的矩阵?

最终结果应包含列Start、bytes、src-subnet、dst-subnet

最佳答案

我认为你可以使用:

g_start_subnet = g_start_subnet.reset_index()

但也许更好的是使用:

print (df.groupby(['Start', 'src-subnet', 'dst-subnet'], as_index=False).sum())

关于python - pandas 按 3 个变量分组,但对其中 2 个变量求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40678742/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com