Reputation: 67
I want to add
I get all values from column:
from collections import Counter
coun_ = set(train_df['time1'].dt.hour)
Then I add new columns to data frame and fill there default values:
for i in coun_:
train_df['hour'+str(i)] = 0
Now I want to get hour from time1
and set 1
to right column. Forexample, if time1
equals 10 then I put 1 to hour10
. I do several ways without success, one of them.
for hour in [train_df]:
hour['hour' + hour['time1'].dt.hour.to_string()] = 1
The question is how I can extract only value from Series and concat it?
Upvotes: 1
Views: 73
Reputation: 863801
Use get_dummies
with DataFrame.add_prefix
adn append to original by DataFrame.join
:
df = df.join(pd.get_dummies(train_df['time1'].dt.hour).add_prefix('hour'))
Upvotes: 1