将python 2D数组转换为pandas数据帧

时间:2018-06-08 18:18:25

标签: python pandas

我正在阅读csv文件,其中每行根据第二个字段的值有不同的解释。每行多次表示相同的类型信息(连续多次重复不同的列值)。我需要逐行阅读并需要将其排列到不同的列表列表或2个数组中。

Like:
with open('info.log','rt') as inputFile:
      filereader = csv.reader(inputFile, delimiter=',')
      for row in filereader:
          if (int(row[1]) == 5):
                for mylist in  (divide the long row into list of lists of fix N length) : # N will be number of columns here
                 array5.append(mylist[2:])   << Now this is an Array of  5 column.
                 # Or Can I append this list direct to dataframe A??

          elif (int(row[1]) == 4):
              for mylist in  (divide the long row into list of lists of fix M length):   # M will be number of columns here
                  array4.append(row[2:3]+mylist)
                  #Can I append this list direct to dataframe B??

解析一个文件后,有2个2D数组。我有多个要解析的文件。 现在我想对该2D阵列进行一些分析。将2D数组转换为pandas数据帧更好吗?

在解析每一行时,我可以将每个已解析的行(列表)附加到panda数据框中吗?

编辑:

添加样本数据: 文件中显示的数据格式如下:

DATA,5,time, <IP1, IP2, write, read,error>, <IP1, IP2, write, read,error>, <IP1, IP2, write, read,error>, <IP1, IP2, write, read,error>....
DATA,4,time, <port_index, read_users, write_users > <port_index, read_users, write_users> <port_index, read_users, write_users>...


DATA,5,20180529235221,::127.0.0.1,127.0.0.1,0,33345,36736,36751,0,::ffff:127.0.0.1,::ffff:127.0.0.1,0,33346,36736,74690,0,::ffff:127.0.0.1,::ffff:127.0.0.1,0,33347,36736,36749,0,
DATA,4,20180529235251,0,4263532,464027, 2,4181981,2060546,3,4181981,2060546, 4,4181981,2060546, 5,4181981,2060546, 6,4263221,464221, 7,418332,206023,8,4181232,2060232, 9,418123,206023, 10,41812323,202456
DATA,5,20180529235221,::127.0.0.1,127.0.0.1,0,33345,36736,36751,0,::ffff:127.0.0.1,::ffff:127.0.0.1,0,33346,36736,74690,0,::ffff:127.0.0.1,::ffff:127.0.0.1,0,33347,36736,36749,0
DATA,5,20180529235221,::127.0.0.1,127.0.0.1,0,33345,36736,36751,0,::ffff:127.0.0.1,::ffff:127.0.0.1,0,33346,36736,74690,0,::ffff:127.0.0.1,::ffff:127.0.0.1,0,33347,36736,36749,0,
DATA,5,20180529235221,::127.0.0.1,127.0.0.1,0,33345,36736,36751,0,::ffff:127.0.0.1,::ffff:127.0.0.1,0,33346,36736,74690,0,::ffff:127.0.0.1,::ffff:127.0.0.1,0,33347,36736,36749,0,
DATA,4,20180529235251,0,4263532,464027, 2,4181981,2060546,3,4181981,2060546, 4,4181981,2060546, 5,4181981,2060546, 6,4263221,464221, 7,418332,206023,8,4181232,2060232, 9,418123,206023, 10,41812323,202456

0 个答案:

没有答案