Can NumPy take care that an array is (nonstrictly) increasing along one axis?

后端 未结 2 1012
北海茫月
北海茫月 2021-01-21 07:36

Is there a function in numpy to guarantee or rather fix an array such that it is (nonstrictly) increasing along one particular axis? For example, I have the following 2D array:<

2条回答
  •  一个人的身影
    2021-01-21 08:00

    pandas offers you the df.cummax function:

    import pandas as pd
    pd.DataFrame(X).cummax(axis=1).values
    
    array([[1, 2, 2, 4, 5],
           [0, 3, 3, 5, 5]])
    

    It's useful to know that there's a first class function on hand in case your data is already loaded into a dataframe.

提交回复
热议问题