1

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?

rnd
  • 67
  • 10

1 Answers1

1

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'))
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • I do this `one_hot = pd.get_dummies(train_df['time1'].dt.hour) train_df = train_df.join(one_hot)` Maybe you know to set custom names for new columns? – rnd Apr 21 '20 at 06:32