gpt4 book ai didi

python - 在列值和常量全局值之间取最小值

转载 作者:太空狗 更新时间:2023-10-29 20:12:18 24 4
gpt4 key购买 nike

我想为给定的数据框创建新列,我在其中计算列值和某个全局值之间的最小值(在本例中为 7)。所以我的 df 有列 sessionnote 我想要的输出列是 minValue :

session     note     minValue
1 0.726841 0.726841
2 3.163402 3.163402
3 2.844161 2.844161
4 NaN NaN

我正在使用内置的 Python 方法 min :

df['minValue']=min(7, df['note'])

我有这个错误:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

最佳答案

使用np.minimum :

In [341]:
df['MinNote'] = np.minimum(1,df['note'])
df

Out[341]:
session note minValue MinNote
0 1 0.726841 0.726841 0.726841
1 2 3.163402 3.163402 1.000000
2 3 2.844161 2.844161 1.000000
3 4 NaN NaN NaN

另外 min 不理解类数组比较因此你的错误

关于python - 在列值和常量全局值之间取最小值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33689714/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com