Pandas Python - Create dummy variables for multiple conditions -


i have pandas dataframe column indicates hour of day particular action performed. df['hour'] many rows each value 0 23.

i trying create dummy variables things 'is_morning', example: if df['hour'] >= 5 , < 12 return 1, else return 0

a loop doesn't work given size of data set, , i've tried other stuff df['is_morning'] = df['hour'] >= 5 , < 12

any suggestions??

you can do:

df['is_morning'] = (df['hour'] >= 5) & (df['hour'] < 12) 

i.e. wrap each condition in parentheses, , use &, and operation works across whole vector/column.


Comments

Popular posts from this blog

c++ - OpenMP unpredictable overhead -

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

javascript - Wordpress slider, not displayed 100% width -