데이터 쓰기 컴퓨팅 모듈

2086 단어 독학 기록
import numpy as np;
import math
import pandas as pd;
import csv

f = open('data.csv','w',newline='',encoding='utf-8-sig')
csv_writer = csv.writer(f)
csv_writer.writerow(["tableName","TcpCount","UdpCount","Other","SrcIpEntropy","DstIpEntropy","SrcPortEntropy","DstPortEntropy","DstPayloadBytes", "DstTotalBytes","DstPacketCount","COUNT"])
def entropy(list):                              # 
    result=-1;
    if(len(list)>0):
        result=0;
    for x in list:
        result=((-x)*math.log(x,2)+result)
    return result;

i=1
while(i < 147):
    t="table"+str(i)
    df=pd.read_csv(t+".csv")
    ILPC=df.loc[:,'ipLayerProtocolCode'].value_counts()             #ipLayerProtocolCode 
    FSSI=df.loc[:,'firstSeenSrcIp'].value_counts(normalize=True)      #firstSeenSrcIp 
    FSSIList=list(FSSI.values)
    FSDI=df.loc[:,'firstSeenDestIp'].value_counts(normalize=True)      #firstSeenDestIp 
    FSDIList=list(FSDI.values)
    FSSP=df.loc[:,'firstSeenSrcPort'].value_counts(normalize=True)      #firstSeenSrcPort 
    FSSPList=list(FSSP.values)
    FSDP=df.loc[:,'firstSeenDestPort'].value_counts(normalize=True)      #firstSeenDestPort 
    FSDPList=list(FSDP.values)
    FSDPB=df.loc[:,'firstSeenDestPayloadBytes'].sum()                    #firstSeenDestPayloadBytes 
    FSDTB=df.loc[:,'firstSeenDestTotalBytes'].sum()                    #firstSeenDestTotalBytes 
    FSDPT=df.loc[:,'firstSeenDestPacketCount'].sum()                    #firstSeenDestPacketCount 
    totalCount=df.loc[:,'firstSeenDestPacketCount']                            # 
    if(len(ILPC)==3):
        x=ILPC[2]
    else:
        x=0
    count=totalCount.shape[0]
    print(i)
    csv_writer.writerow([t,ILPC[0],ILPC[1],x,entropy(FSSIList),entropy(FSDIList),entropy(FSSPList),entropy(FSDPList),FSDPB, FSDTB,FSDPT,count])
    i=i+1
f.close()
print("1")




좋은 웹페이지 즐겨찾기