How to multiply a scalar throughout a specific column within a NumPy array?

后端 未结 3 1464
死守一世寂寞
死守一世寂寞 2021-02-05 02:38

I need to do some analysis on a large dataset from a hydrolgeology field work. I am using NumPy. I want to know how I can:

  1. multiply e.g. the 2nd column of my ar

3条回答
  •  鱼传尺愫
    2021-02-05 03:30

    Sure:

    import numpy as np
    # Let a be some 2d array; here we just use dummy data 
    # to illustrate the method
    a = np.ones((10,5))
    # Multiply just the 2nd column by 5.2 in-place
    a[:,1] *= 5.2
    
    # Now get the cumulative sum of just that column
    csum = np.cumsum(a[:,1])
    

    If you don't want to do this in-place you would need a slightly different strategy:

    b = 5.2*a[:,1]
    csum = np.cumsum(b)
    

提交回复
热议问题