Search code examples
pythonnumpymatrix-multiplicationmultiplication

Is there a method to multiply only certain elements in a numpy array


Suppose I have a numpy array like so:

a = ([[4, 9], [38, 8], [90, 10]...[8545, 17]])

Where the first element is a location ID and the second is the amount of time spent at each location in minutes. I want to convert these times into seconds which requires me to multiply every other value by 60.

As this is a very long array, what would be the most time-efficient method for converting these times?


Solution

  • Use this:

    import numpy as np
    # a is your original list of [location, time] pairs
    a = np.array(a)
    a[:, 1] *= 60
    

    It simply multiplies the second column of your array a by 60 to convert the time values into seconds.