Pandas: Ternary conditional operator for setting a value in a DataFrame(Pandas:用于在 DataFrame 中设置值的三元条件运算符)
问题描述
我有一个数据框 pd.我想更改列 irr 的值,具体取决于它是高于还是低于阈值.
I have a dataframe pd. I would like to change a value of column irr depending on whether it is above or below a thresh hold.
我怎样才能在一行中做到这一点?现在我有
How can I do this in a single line? Now I have
pd['irr'] = pd['irr'][pd['cs']*0.63 > pd['irr']] = 1.0
pd['irr'] = pd['irr'][pd['cs']*0.63 <= pd['irr']] = 0.0
问题当然是我改了irr,在下一行再次检查.
The problem of course is that I change irr and check it again in the next line.
是否有类似 pandas 的三元条件运算符?
Is there something like a ternary conditional operator for pandas?
推荐答案
在 pandas 中没有,在 numpy 中是.
In pandas no, in numpy yes.
您可以使用 numpy.where 或将条件创建的 boolean Series 转换为 float - True 是 1.0 和 >Falses 是 0.0:
You can use numpy.where or convert boolean Series created by condition to float - Trues are 1.0 and Falses are 0.0:
pd['irr'] = np.where(pd['cs']*0.63 > pd['irr'], 1.0, 0.0)
或者:
pd['irr'] = (pd['cs']*0.63 > pd['irr']).astype(float)
示例:
pd = pd.DataFrame({'cs':[1,2,5],
'irr':[0,100,0.04]})
print (pd)
cs irr
0 1 0.00
1 2 100.00
2 5 0.04
pd['irr'] = (pd['cs']*0.63 > pd['irr']).astype(float)
print (pd)
cs irr
0 1 1.0
1 2 0.0
2 5 1.0
这篇关于Pandas:用于在 DataFrame 中设置值的三元条件运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Pandas:用于在 DataFrame 中设置值的三元条件运算符
基础教程推荐
- 究竟什么是“容器"?在蟒蛇?(以及所有的 python 容器类型是什么?) 2022-01-01
- matplotlib 设置 yaxis 标签大小 2022-01-01
- Python 中是否有任何支持将长字符串转储为块文字或折叠块的 yaml 库? 2022-01-01
- 比较两个文本文件以找出差异并将它们输出到新的文本文件 2022-01-01
- 在 Python 中将货币解析为数字 2022-01-01
- Kivy 使用 opencv.调整图像大小 2022-01-01
- Python,确定字符串是否应转换为 Int 或 Float 2022-01-01
- 在 Django Admin 中使用内联 OneToOneField 2022-01-01
- kivy 应用程序中的一个简单网页作为小部件 2022-01-01
- 对多索引数据帧的列进行排序 2022-01-01
