简体   繁体   English

Pandas bin 和赋值

[英]Pandas bin and assign values

I have a score card with many variables and i am trying to calculate scores with it using python我有一张包含许多变量的记分卡,我正在尝试使用 python 计算分数

This is how my score card looks for one variable:这就是我的记分卡查找一个变量的方式:

Bins      score    
missing     2
[-Inf,20)   2
[20,40)     0
[40,140)   -1
[140, Inf) -2

This is how my data looks for that variable:这就是我的数据查找该变量的方式:

ID_num Quick_Ratio
273N    61.53
280G    34.39
119D    0.00
080R    168.91
177K    53.97
204N    110.80
1902R   0.00
2035H   NaN
035G    58.74
3479Z   182.13

this is what i am doing now:这就是我现在正在做的事情:

#bin the columns first
cut_labels = ['[-Inf,20)', '[20,40)', '[40,140)', '[140,inf)']
cut_bins = [-np.inf,20, 40, 140, np.inf]

df['quick_ratio_bin']= pd.cut(df['Quick_Ratio'], bins=cut_bins, labels=cut_labels, include_lowest=True)
df['quick_ratio_bin']=(df['quick_ratio_bin'].cat.add_categories('missing').fillna('missing'))


df.loc[df['quick_ratio_bin'] == 'missing', 'quick_ratio_s'] = 2
df.loc[df['quick_ratio_bin'] == '[-Inf,20)', 'quick_ratio_s'] = 2
df.loc[df['quick_ratio_bin'] == '[20,40)', 'quick_ratio_s'] = 0
df.loc[df['quick_ratio_bin'] == '[40,140)', 'quick_ratio_s'] = -1
df.loc[df['quick_ratio_bin'] == '[140,inf)', 'quick_ratio_s'] = -2

Is there a better way to do this?有一个更好的方法吗?

cut_bins = [-np.inf,20, 40, 140, np.inf]
score_labels = [2,0,-1,-2]
m1['quick_ratio_s']= pd.cut(m1['Quick_Ratio'], 
                            bins=cut_bins, 
                            labels=score_labels).astype(float).replace(np.nan,2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM