2017-08-16 73 views
0

我用下面的代码,以分割.csv文件添加额外的列取决于主要的.csv列8:劈裂的.csv到每个新的.csv

import csv 
import pandas as pd  

def spliteCsv(input,output): 
    print(input) 
    data=set() 
    with open (input) as csvfile: 
     file = csv.reader (csvfile,delimiter=',') 
     next (file,None) 
     for row in file: 
      if row[7] =='': 
       data.add (-1) 
      else: 
       data.add (int(row[7])) 

    data = list(data) 
    ofile = pd.read_csv (input, sep=',') 
    data.append(max(data)+1) 
    for d in data: 
     csv_temp = ofile[ofile['col8'].fillna (max(data)).astype(int) == d] 
     csv_temp.to_csv ('%s_%s.csv'%(output,d),sep=',') 
    return 

这里是我需要:

col1 col2 col3 col4 col5 col6 col7 col8 col9 
1  a  k8       5 
2  j  l9       5 
3  k  o0       5 
4  l  m7       5 

这里是代码输出:

col0 col1 col2 col3 col4 col5 col6 col7 col8 col9 
0  1  a  k8       5 
1  2  j  l9       5 
2  3  k  o0       5 
3  4  l  m7       5 

你明白,它插入其他列包含第一列

编辑:

source.csv:

frame.number frame.time_epoch  ip.src   ip.dst  tcp.srcport  tcp.dstport  tcp.seq  tcp.stream  frame.len  tcp.flags  _ws.col.Info 
    1   1501756607   192.168.1.10 37.48.64.201  47159   7095  1    1   215    0x00000018  47159 → 7095 [PSH, ACK] Seq=1 Ack=1 Win=2235 Len=149 TSval=19928932 TSecr=2777283254 
    2   1501756607   37.48.64.201 192.168.1.10  7095   47159  1    2   66    0x00000010  7095 → 47159 [ACK] Seq=1 Ack=150 Win=91 Len=0 TSval=2777285491 TSecr=19928932 
    3   1501756607   37.48.64.201 192.168.1.10  7095   47159  1    1   215    0x00000018  7095 → 47159 [PSH, ACK] Seq=1 Ack=150 Win=91 Len=149 TSval=2777285491 TSecr=19928932 
    4   1501756607   192.168.1.10 37.48.64.201  47159   7095  150    2   215    0x00000018  47159 → 7095 [PSH, ACK] Seq=150 Ack=150 Win=2235 Len=149 TSval=19928977 TSecr=2777285491 
    5   1501756607   192.168.1.10 37.48.64.201  47159   7095  299    2   343    0x00000018  47159 → 7095 [PSH, ACK] Seq=299 Ack=150 Win=2235 Len=277 TSval=19928979 TSecr=2777285491 
    6   1501756607   37.48.64.201 192.168.1.10  7095   47159  150       66    0x00000010  7095 → 47159 [ACK] Seq=150 Ack=576 Win=91 Len=0 TSval=2777285537 TSecr=19928977 

输出文件:

文件1:

frame.number frame.time_epoch  ip.src   ip.dst  tcp.srcport  tcp.dstport  tcp.seq  tcp.stream  frame.len  tcp.flags  _ws.col.Info 
    1   1501756607   192.168.1.10 37.48.64.201  47159   7095  1    1   215    0x00000018  47159 → 7095 [PSH, ACK] Seq=1 Ack=1 Win=2235 Len=149 TSval=19928932 TSecr=2777283254 
    3   1501756607   37.48.64.201 192.168.1.10  7095   47159  1    1   215    0x00000018  7095 → 47159 [PSH, ACK] Seq=1 Ack=150 Win=91 Len=149 TSval=2777285491 TSecr=19928932 

文件2:

frame.number frame.time_epoch  ip.src   ip.dst  tcp.srcport  tcp.dstport  tcp.seq  tcp.stream  frame.len  tcp.flags  _ws.col.Info 
    2   1501756607   37.48.64.201 192.168.1.10  7095   47159  1    2   66    0x00000010  7095 → 47159 [ACK] Seq=1 Ack=150 Win=91 Len=0 TSval=2777285491 TSecr=19928932 
    4   1501756607   192.168.1.10 37.48.64.201  47159   7095  150    2   215    0x00000018  47159 → 7095 [PSH, ACK] Seq=150 Ack=150 Win=2235 Len=149 TSval=19928977 TSecr=2777285491 
    5   1501756607   192.168.1.10 37.48.64.201  47159   7095  299    2   343    0x00000018  47159 → 7095 [PSH, ACK] Seq=299 Ack=150 Win=2235 Len=277 TSval=19928979 TSecr=2777285491 

文件3:

frame.number frame.time_epoch  ip.src   ip.dst  tcp.srcport  tcp.dstport  tcp.seq  tcp.stream  frame.len  tcp.flags  _ws.col.Info 
    6   1501756607   37.48.64.201 192.168.1.10  7095   47159  150    3   66    0x00000010  7095 → 47159 [ACK] Seq=150 Ack=576 Win=91 Len=0 TSval=2777285537 TSecr=19928977 
+0

我想有一个更好的(__vectorized__)的方式来做到这一点?你能发布你的源CSV吗,根据你的问题你会得到所需的数据集? – MaxU

+0

以csv格式捕获的流量,以下是他的头文件:frame.number,frame.time_epoch,ip.src,ip.dst,tcp.srcport,tcp.dstport,tcp.seq,tcp.stream,frame.len,tcp .flags,_ws.col.Info。全部保存在字符串中。我想分割取决于tcp.stream号码。 – user3806649

+0

你可以[更新你的问题](https://stackoverflow.com/posts/45722843/edit)与相应的样本数据集和你想要的(结果)数据集? – MaxU

回答

2

使用index=False参数:

csv_temp.to_csv ('%s_%s.csv'%(output,d),sep=',', index=False) 
# NOTE:           ^^^^^^^^^^^ 

UPDATE:

df = pd.read_csv('/path/to/source/file.csv') 

df['tcp.stream'] = pd.to_numeric(df['tcp.stream'], errors='coerce').fillna(-1) 

# please set desired path and file name in the next line 
output_path_template = 'd:/temp/tcp.stream.{}.csv' 

df.groupby('tcp.stream') \ 
    .apply(lambda x: x.to_csv(output_path_template.format(x.name), index=False)) 
+0

某些行缺少tcp.stream列的值,在更新的解决方案中,这些数据被省略。 – user3806649

+0

@ user3806649,现在应该修复... – MaxU