I am trying to convert this dataframe:
data_in = {
'dates': [2017, 2017, 2018, 2019, 2019, 2019],
'names': ['Roger', 'Rafa', 'Roger', 'Rafa', 'Novak', 'Dom']
}
df_in = pd.DataFrame(data_in)
>>> df_in
dates names
0 2017 Roger
1 2017 Rafa
2 2018 Roger
3 2019 Rafa
4 2019 Novak
5 2019 Dom
into this binary matrix:
>>> df_out
Roger Rafa Novak Dom
dates
2017 1 1 0 0
2018 1 0 0 0
2019 0 1 1 1
with all the dates as index, all the names as columns, and the data being 1 if occurence of the name at the date, and np.NaN or 0 if not.
I can build the df_out dataframe with its index and columns, but how would you get the data?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…