我有一个样品
我需要创建另一个列C,它根据一些断点来存储列B
Breakpts=[50100250350]
我有以下代码可以工作
def conditions(i):
if i <=50: return '0-50'
if i > 50 and i <=100: return '50-100'
if i > 100 and i <=250: return '100-250'
if i > 250 and i <=350: return '250-350'
if i > 350: return '>350'
df['C']=df['B'].apply(conditions)
然而,我想让这一切变得疯狂。因此,如果我使用不同的breakpts,比如[100250300400],代码应该基于breakpts自动创建不同的bucket
有什么办法吗
正如评论中指出的那样,
pd.cut()
将是一条道路。您可以使分手动态化,并自行设置:再看一下^{} ,它是一个基于分位数的离散化函数
或者,使用
np.select
:两种印刷品:
相关问题 更多 >
编程相关推荐