Search code examples
pythonnumpy

numpy function similar to pandas update?


Is it possible to perform a matrix update in numpy similar to how pandas can update a dataframe.

In pandas if I have two dataframes, I can filter one for the update values and then apply the dataframe to the first with the update() function.

    df = pd.DataFrame({'A': [1, 2, 3],
                   'B': [400, 500, 600]})
    new_df = pd.DataFrame({'B': [4, 5, 6]})

    df.update(new_df[new_df["B"] > 4])

I do not know if numpy has a similar function. Currently, I have to loop through the array at each element.

    narr = np.random.rand(10)
    new_narr = np.random.rand(10)
    for i, c in enumerate(new_narr > narr):
         if c:
             narr[i] = new_narr[i]

Solution

  • You can do the action in your second clip using fancy indexing:

    import numpy as np
    narr = np.random.rand(10)
    new_narr = np.random.rand(10)
    print(narr)
    print(new_narr)
    narr[new_narr > narr] = new_narr[new_narr > narr]
    print(narr)
    

    I did the prints so you can see that it took effect. Output:

    [0.101018   0.92010774 0.0168271  0.25279981 0.37062389 0.58470343
     0.41127726 0.47887049 0.63323112 0.48178234]
    [0.17995055 0.39193739 0.7586071  0.72915965 0.48462354 0.63462616
     0.02083988 0.19413093 0.37365804 0.73229754]
    [0.17995055 0.92010774 0.7586071  0.72915965 0.48462354 0.63462616
     0.41127726 0.47887049 0.63323112 0.73229754]
    

    Of course, this particular example would be better done with np.max, but let's assume this was part of something more complicated.