将函数应用于数据框列熊猫

时间:2018-10-16 07:10:36

标签: pandas function dataframe apply

我目前创建了一个应用于熊猫数据框的函数,如下所示:

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 

并应用于数据框:

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

当我尝试此操作时,我得到的只是“夜晚”。有什么建议吗?

谢谢

1 个答案:

答案 0 :(得分:2)

cutfillna一起使用:

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