weather_train=pd.DataFrame({
'site_id':[0,0,0,0,0,0,1,1,1,1,1],
'air_temperature':[25,22,21,28,29,30,45,48,50,22,24]
}
)
我想在air_temperature列之后添加一个名为“ Seasons”的新列。条件是,如果air_temperature小于或等于25,则应在Seasons列中输出“ winter”,如果air_temperature在25和45之间,则应在Seasons列中输出“ summer”,如果air_temperature大于或等于45,则应在“季节”列中输出“夏季”
图片
我发现在R中执行此操作非常容易。但是我该如何在熊猫上做到这一点?
答案 0 :(得分:0)
您要将连续数据转换为分类数据。为此,我们使用pd.cut
:
weather_train['Seasons'] = pd.cut(weather_train['air_temperature'],
bins=[-float('inf'), 25, 45, float('inf')],
labels=['winter', 'spring', 'summer'])
site_id air_temperature Seasons
0 0 25 winter
1 0 22 winter
2 0 21 winter
3 0 28 spring
4 0 29 spring
5 0 30 spring
6 1 45 spring
7 1 48 summer
8 1 50 summer
9 1 22 winter
10 1 24 winter