Imagine a pandas
dataframe that are given by
df = pd.DataFrame({
\'id\': [1, 1, 1, 2, 2],
\'location\': [1, 2, 3, 1, 2],
\'date\': [p
get_dummies
df.join(pd.get_dummies(df.date.dt.year).sum(level=0))
date location 2015 2016 2017 2018
id
1 2015-01-01 1 2 1 0 0
1 2016-01-01 2 2 1 0 0
1 2015-01-01 3 2 1 0 0
2 2017-01-01 1 0 0 1 1
2 2018-01-01 2 0 0 1 1
factorize
i, r = pd.factorize(df.index)
j, c = pd.factorize(df.date.dt.year)
n, m = shape = len(r), len(c)
b = np.zeros(shape, dtype=np.int64)
np.add.at(b, (i, j), 1)
df.join(pd.DataFrame(b, r, c).rename_axis('id'))
date location 2015 2016 2017 2018
id
1 2015-01-01 1 2 1 0 0
1 2016-01-01 2 2 1 0 0
1 2015-01-01 3 2 1 0 0
2 2017-01-01 1 0 0 1 1
2 2018-01-01 2 0 0 1 1