I have a datafame that looks like this:
import pandas as pd
df = pd.DataFrame(
{'ID': ['1', '1', '1', '1', '1',
'2' , '2', '3', '3'],
'Year': ["2012", "2013", "2014", "2015",
"2016", "2012", "2013", "2012", "2013"],
'Event': ['0', '0', '0', '1','0', '0',
'0', '1', '0']})
I want to create a new column where the values are centered around the event such that the time before the event is decreasing from 0, the time of the event is 0, and the time after the event is increasing from 0. In each case, time before and after the event would only be recorded for each ID. Some ID's do not have an event so they remain 0 and each event can only happen a maximum of one time for each ID.
I would like for the result to look like this:
out = pd.DataFrame(
{'ID': ['1', '1', '1', '1', '1',
'2', '2', '3', '3'],
'Year': ["2012", "2013", "2014", "2015",
"2016", "2012", "2013", "2012",
"2013"],
'Event': ['0', '0', '0', '1','0', '0',
'0', '1', '0'],
'Period': ['-3', '-2', '-1', '0',
'1', '0', '0', '0', '1']})
Any thoughts? Thank you in advance!