Sami
Sami

Reputation: 1

Time of day Python

for i in range(len(df)):
    if df.loc[i, 'Hour'] >=6 & df.loc[i, 'Hour'] <= 12:
        df['time_of_day'] = "Morning"

    elif df.loc[i, 'Hour'] > 12 & df.loc[i, 'Hour'] <= 17:
        df['time_of_day'] == "Afternoon"
        
    elif df.loc[i, 'Hour'] > 17 & df.loc[i, 'Hour'] <= 22:
        df['time_of_day'] == "Evening"
    
    else:
        df['time_of_day'] = 'Night' 

df.time_of_day.value_counts()   

Output: Morning 1428 Name: time_of_day, dtype: int64

I try to create time_of_day column with for loop and if statement. Everything seems true, but it's not correctly working, the whole column is becoming "Morning".

Upvotes: 0

Views: 75

Answers (1)

StonedTensor
StonedTensor

Reputation: 616

When you call df['time_of_day'] = "Morning" , you are assigning "morning" to every value in that column. What you really want to do is apply your logic to each row separately. The smartest way to do this is with the apply method. All you need to do is define a function for your logic beforehand.


def determine_time(hour):

    if hour >=6 and hour <= 12:
        return "Morning"

    elif hour > 12 and hour <= 17:
        return "Afternoon"
        
    elif hour > 17 and hour <= 22:
        return "Evening"
    
    else:
        return 'Night' 

df['time_of_day'] = df['hour'].apply(determine_time)


Two more things, avoid using "&" as as it has a different meaning than and in Python. Lastly, = is for variable assignment but == checks if two things are equal (as in a Boolean expression)

Upvotes: 1

Related Questions