how do I loop this in pandas instaed of writning 44 times?
df.loc[(df['id']!=df['id'].shift(-1)),['q1']]=np.nan
df.loc[(df['id']!=df['id'].shift(-2)),['q2']]=np.nan
df.loc[(df['id']!=df['id'].shift(-3)),['q3']]=np.nan
CodePudding user response:
You should use a for loop, which will substitute in a new value for i each time. I've done it up to 3 (as per your example), but from you question, I think this should go to 44.
for i in range(1, 3):
df.loc[(df['id']!=df['id'].shift(-i)),['q' str(i)]]=np.nan