How to Convert scapy packetList to dataframe in python?

Viewed 969

I am trying to convert(pcap data) packetList to dataframe so that i can perform preprocessing on it. I have used scapy library to read pcap file.

import pandas as pd
from scapy.all import *

packets_list=rdpcap('D:/anaconda/path/youtube2.pcap')    
//return type PacketList 

print(packets_list[3].show())    //Display contents of the packet

df = pd.DataFrame (packets_list,columns= 
['No.','Time','Source','Destination','Protocol','Length','Info'])

print (df)

ERROR:

TypeError: DataFrame constructor called with incompatible data and dtype: cannot copy sequence with size 66 to array axis with dimension 1

Please help anyone.

1 Answers

I used something like this:

fields = {
    UDP: ('sport', 'dport'),
    # ...
}

df = pd.DataFrame({field: [getattr(pkt[layer], field) if layer in pkt else pd.NA
                           for pkt in packets_list]
                   for layer, field_list in fields.items()
                   for field in field_list})
Related