I'm trying to build a dataframe in python that is filled with 1s and 0s, depending on the number in one column:
Date Hour
2005-01-01 1
2005-01-01 2
2005-01-01 3
2005-01-01 4
I want to make new columns based on the number in "Hour", and fill each column with a 1 if that row is equal to the value in "Hour", or 0 if not.
Date Hour HE1 HE2 HE3 HE4
2005-01-01 1 1 0 0 0
2005-01-01 2 0 1 0 0
2005-01-01 3 0 0 1 0
2005-01-01 4 0 0 0 1
I can do it with this code, but it takes a long time:
for x in range(1,5):
_HE = 'HE' + str(x)
for i in load.index:
load.at[i, _HE] = 1 if load.at[i,'Hour']==x else 0
I feel like this is a great application (no pun intended) for .apply(), but I can't get it to work right.
How would you speed this up?