I have a time sorted pandas dataframe, where the columns are dates with boolean values as the rows as to whether a person was present at that date. If they are, I want to persist that 'present' to all the following columns (the columns are sorted chronologically).
I've reduced the problem to a simpler numpy problem. Say I have ndarray:
ndarr = np.array([[0.0, 0.0, 1.0], [1.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 1.0, 0.0]]) array([[ 0., 0., 1.], [ 1., 0., 0.], [ 1., 0., 0.], [ 0., 1., 0.], [ 0., 1., 0.]]) How do I make it so that if a one appears in one column, it's persisted to the right?
My current solution iterates over the columns in python and I'm wondering whether there is a more elegant solution.
Current solution:
nd_store = np.ones(ndarr.shape[0]) for i in reversed(range(ndarr.shape[1])): tmp = np.copy(ndarr[:,i]) ndarr[:,i] = nd_store nd_store = (tmp != nd_store) * 1.0 array([[ 0., 0., 1.], [ 1., 1., 1.], [ 1., 1., 1.], [ 0., 1., 1.], [ 0., 1., 1.]])