python - 我可以将 pandas 的 bin 指定为我的数据框中的列吗?
问题描述
希望这是一个非常简单的方法,但是对于用于解决我的问题的正确 pandas 方法,我有点难过。
我正在尝试根据它们是否低于、介于或高于其他两列(Limit1 和 Limit2)中的值来评估数据框中“值”列中的数字的波段。例如 :
Value Limit1 Limit2 Band
3 2 5
5 6 7
5 4 8
9 6 7
2 4 5
如果我将 bin 指定为单个数字,pd.cut 可以工作,但我想将 bin 指定为我的数据框中的列,以便每一行都有自己的特定 bin,如下所示
df['Band'] = df.apply(lambda x: pd.cut(x.value, bins=[0, x.Limit1, x.Limit2, np.inf], labels=['Band1','Band2','Band3']))
这失败了,因为我提供了一个系列,其中 cut 函数需要一个数字。谁能建议我如何使用 pd.cut 做到这一点,或者我应该完全使用不同的 pandas 函数?
我宁愿避免 np.where,因为我可能不得不将 bin 扩展到五个或六个,并且我不想拥有嵌套代码。
提前谢谢了!
解决方案
让我们尝试np.select
:
m1 = df['Value'].lt(df['Limit1'])
m2 = df['Value'].gt(df['Limit2'])
df['Band'] = np.select([m1, m2], ['band1', 'band3'], 'band2')
Value Limit1 Limit2 Band
0 3 2 5 band2
1 5 6 7 band1
2 5 4 8 band2
3 9 6 7 band3
4 2 4 5 band1