rnd
rnd

Reputation: 67

Extract value from Series

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

Answers (1)

jezrael
jezrael

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

Related Questions