I have an indicator variable in my dataframe that takes on the values 1 0 or -1. I'd like to create a new variable that avoids the 0's and instead repeats the nonzero values of the indicator variable until it changes to 1 or -1.
I tried various constructions using the np.where statement, but I cannot solve this problem.
Here is the original dataframe:
import pandas as pd
df = pd.DataFrame(
{'Date': [1,2,3,4,5,6,7,8,9,10],
'Ind': [1,0,0,-1,0,0,0,1,0,0]})
df
I am hoping to get a dataframe that looks like the following:
df2 = pd.DataFrame(
{'Date': [1,2,3,4,5,6,7,8,9,10],
'Ind': [1,0,0,-1,0,0,0,1,0,0],
'NewVar':[1,1,1,-1,-1,-1,-1,1,1,1]})
Aucun commentaire:
Enregistrer un commentaire