NZ_DJ
NZ_DJ

Reputation: 341

Applying a function to a dataframe column pandas

I currently created a function to apply to a pandas dataframe, as below:

def timebin (col):
timebin = []
for x in range(len(col)):
    if x>=0 & x<5:
        return 'night'
    elif x>5 & x<9:
        return 'early_morning'
    elif x>8 & x<12:
        return 'morning'
    elif x>11 & x<15:
        return 'afternoon'
    elif x>14 & x<18:
        return 'late_afternoon'
    elif x>17 & x<21:
        return 'evening'
    else:
        return 'night'

    timebin.append(x)

return timebin 

And applying to a dataframe:

df['new_col'] = timebin(df['col'])

When I try this all I get returned is 'night'. Any suggestions?

Thanks

Upvotes: 1

Views: 70

Answers (1)

jezrael
jezrael

Reputation: 862406

Use cut with fillna:

df = pd.DataFrame({
    'col': list(range(24))
})

bins = [0,5,9,12,15,18,21]
lab = ['night','early_morning','morning','afternoon','late_afternoon','evening']
df['new_col'] = pd.cut(df['col'], bins=bins, labels=lab, right=False).fillna('night')
print (df)
    col         new_col
0     0           night
1     1           night
2     2           night
3     3           night
4     4           night
5     5   early_morning
6     6   early_morning
7     7   early_morning
8     8   early_morning
9     9         morning
10   10         morning
11   11         morning
12   12       afternoon
13   13       afternoon
14   14       afternoon
15   15  late_afternoon
16   16  late_afternoon
17   17  late_afternoon
18   18         evening
19   19         evening
20   20         evening
21   21           night
22   22           night
23   23           night

Upvotes: 2

Related Questions