在 for 循环中合并多个数据集 (Python)

时间:2021-05-21 14:26:30

标签: python dataframe loops merge concatenation

我正在尝试使用以下 for 循环合并许多数据集,但我不断收到错误。

for x in range(5):
    for i in range(2):
        if (arp_df[x,i]["layer"] == "eth"):
            df_eth = pd.concat([df[x,i]["layer"] == "eth"])

        elif (arp_df[x,i]["layer"] == "vlan"):
            df_vlan = pd.concat([df[x,i]["layer"] == "vlan"])

        else:
            df_icmp = pd.concat([df[x,i]["layer"] == "icmp"])

我有以下数据集:

df[0,0]:
eth.source: 192.28
eth.destination: 128.33
eth.mac.source: 12:a2
eth.mac.destination: 12:a2
eth.layer: eth

df[0,1]:
icmp.source: 191.33
icmp.destination: 167.25
icmp.mac.source: 08:b7
icmp.mac.destination: ab:23
eth.layer: icmp

df[0,2]:
vlan.source: 190.33
vlan.destination: 153.24
vlan.mac.source: 09:a0
vlan.mac.destination: e0:72
eth.layer: vlan

df[1,0]:
eth.source: 167.30
eth.destination: 112.62
eth.mac.source: 08:ab
eth.mac.destination: 0e:a2
eth.layer: eth

df[1,1]:
icmp.source: 127.12
icmp.destination: 112.33
icmp.mac.source: 06:ef
icmp.mac.destination: 12:ae
eth.layer: icmp

df[1,2]:
vlan.source: 97.112
vlan.destination: 129.12
vlan.mac.source: 08:b7
vlan.mac.destination: ab:23
eth.layer: vlan

df[2,1] ... to df[10,2]

我想根据列合并它们:“层”使用以下格式:

df[0]:
eth.source: 192.28, 167.3, ...
eth.destination: 128.33, 127.12, ...
eth.mac.source: 12:a2, 08:ab, ...
eth.mac.source: 12:a2, 0e:a2, ...
eth.layer: eth

df[1]:
icmp.source: 191.33, 127.12, ...
icmp.destination: 167.25, 112.33, ...
icmp.mac.source: 08:b7, 08:ab, ...
icmp.mac.destination: ab:23, 12:ae, ...
eth.layer: icmp

df[2]:
vlan.source: 190.33, 97.112, ...
vlan.destination: 153.24, 129.12, ...
vlan.mac.source: 09:a0, 08:b7, ...
vlan.mac.destination: e0:72, ab:23, ...
eth.layer: vlan

考虑到有时图层不存在(例如df[3,1]) 并且数据集需要按以下顺序如下所示:

df[1]:
vlan.source: 190.33, 97.112, N/A, 114.7, ...
vlan.destination: 153.24, 129.12, N/A, 94.12, ...
vlan.mac.source: 09:a0, 08:b7, N/A, cb:00, ...
vlan.mac.destination: e0:72, ab:23, N/A, ba: 28, ...
eth.layer: icmp

请问有什么帮助吗? :D(请记住,顺序很重要!)

0 个答案:

没有答案
相关问题