我想为给定的数据框创建新列,我在其中计算列值和某个全局值之间的最小值(在本例中为 7)。所以我的 df 有列 session
和 note
我想要的输出列是 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/