python - Efficiently splitting a column of numpy array -


I have a CSV file with 5 columns, in which the second column time format 10/22/1414 : 00 I want to create another file with different data divided into different columns this time. I used the following code to split the column

  imported import loadex import import number as np time import stripteam file name = 'data / file.csv' data = loadtxt (File name, delimiter = ',', dtype = str, skiprows = 1) newdata = np.zeros ((data.shape [0], 7)) newdata [, 0] = data [, 0] In (lane (data [:, 1]): TM = striptime (data [i, 1], "% m /% d /% y% h:% m") new data [i, 1] = tmtm_yure New data [I, 2] = Tm.tm_wday newdata [i, 3] = tm.tm_hour newdata [,, 4:] = data [,, 2:]  

Is there a better way to use Numpy methods or other modules of python?

You can reduce the generation of using the following three lines newdata :

  1. Data changes data time objects into strings:

     
    for D in the data < P> I think that you use from da toetime import datetime .

  2. Collect every day-time item of year, day of week and hour.

      meaningful date: [dt.year, dt.weekday (), dt.hour] for dt in datetimes)  
  3. Stack all the horizontal parts together: the first column of the original data , date and time information as well as the data Last column.

      newdata = np.hstack ((data [, 0, none], year-long, data [,, 2:]))  

    Note that the first column is an additional none is not indexed to obtain a 2D array, which is necessary for horizontal stacking.


I am sure that if this is an better solution you can be more readable, especially those all python list Are not quite familiar with the understanding. But this may be an optional price reading and playing with the surroundings. It can be a very powerful tool.


Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -