将函数应用于数据框列 pandas [英] Applying a function to a dataframe column pandas
本文介绍了将函数应用于数据框列 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
并应用于数据框:
df['new_col'] = timebin(df['col'])
当我尝试这个时,我得到的只是晚上".有什么建议吗?
When I try this all I get returned is 'night'. Any suggestions?
谢谢
推荐答案
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
这篇关于将函数应用于数据框列 pandas 的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文