csv - Copying data to another column in Python/Pandas -
i have ip packet csv file i'm trying extract sequence numbers info field separate column has sequence numbers. sequence numbers substring in middle of info string. here raw code.. first create new column fo sequence numbers, check if info field contains seq number, split info field sequence number out. if print after 'seq = j.split...', correct values. how write csv file seq column?
file = pd.read_csv(file.csv) file['seq'] = none in file['info']: if 'seq' in i: split = i.split(' ') j in split: if 'seq=' in j: seq = j.split('seq=',1)[1] file.loc[i,'seq'] = int(seq)
example csv:
no. time source destination protocol length info 1 0.000000 sourceip 192.168.0.1 tcp 54 35165 > 80 [syn] seq=0 win=16384 len=0 2 0.000001 sourceip 192.168.0.1 tcp 54 14378 > 80 [syn] seq=0 win=16384 len=0 3 0.000003 sourceip 192.168.0.1 tcp 54 31944 > 80 [syn] seq=0 win=16384 len=0
desired outcome:
no. time source destination protocol length info seq 1 0.000000 sourceip 192.168.0.1 tcp 54 35165 > 80 [syn] seq=0 win=16384 len=0 0 2 0.000001 sourceip 192.168.0.1 tcp 54 14378 > 80 [syn] seq=0 win=16384 len=0 0 3 0.000003 sourceip 192.168.0.1 tcp 54 31944 > 80 [syn] seq=0 win=16384 len=0 0
use str.extract
file['seq'] = file.info.str.extract('seq=(\d+)', expand=false).astype(float)
Comments
Post a Comment