I am trying to replace every instance of 2 (except for the first instance of 2), with the value 0. This is the code I tried, which results in an error message. Best solution I could think of was .where() but also could see maybe a duplicates() code that keeps='first'. Note I want to call all columns without specifying each individual column since the dataframe is much bigger. If you look at the first column, where it shows 2020-08 at the bottom, I would like for that to be a '0'.
original output:
pd.DataFrame({'year_month': [2018-02, 2018-03, 2018-04, 2018-05, 2018-06, 2018-07],
'adoption_1': [0, 0, 1, 1, 1, 2, 2],
'adoption_2': [0, 0, 0, 1, 2, 2, 2],
'adoption_3': [0, 1, 1, 1, 1, 2, 2})
df.set_index('year_month')
desired output:
pd.DataFrame({'year_month': [2018-02, 2018-03, 2018-04, 2018-05, 2018-06, 2018-07],
'adoption_1': [0, 0, 1, 1, 1, 2, 0],
'adoption_2': [0, 0, 0, 1, 2, 0, 0],
'adoption_3': [0, 1, 1, 1, 1, 2, 0})
df.set_index('year_month')
df[df.where((df.shift(2) == 1) & (df.shift(1) == 2))] = 0