[英]Pandas - if X float in column is greater than Y, find difference between X and Y and multiply by .25
I suspect the solution is quite simple, but I have been unable to figure it out.我怀疑解决方案很简单,但我一直无法弄清楚。 Essentially, what I want to do is to query a column with the float object type to see if each value >= 100.00.本质上,我想要做的是查询具有浮点对象类型的列,以查看每个值是否>= 100.00。 If it is greater, then I want to take the value x and do so: ((x - 100)*.25)+100 = new value (replace original values inplace, preferably.)如果它更大,那么我想取值 x 并这样做: ((x - 100)*.25)+100 = 新值(最好就地替换原始值。)
The data looks something like:数据看起来像:
Some columns here这里有一些专栏 | A percentage stored as float存储为浮点数的百分比 |
---|---|
foobar食物吧 | 84.85 84.85 |
foobar食物吧 | 15.95 15.95 |
fuubahr富巴尔 | 102.25 102.25 |
The result of the above operation mentioned would give the following for the above:上述操作的结果将给出上述内容:
Some columns here这里有一些专栏 | A percentage stored as float存储为浮点数的百分比 |
---|---|
foobar食物吧 | 84.85 84.85 |
foobar食物吧 | 15.95 15.95 |
fuubahr富巴尔 | 100.5625 100.5625 |
Thanks!谢谢!
List comprehension is easy solution for this:列表理解是一个简单的解决方案:
dataframe["A percentage stored as float"] = [((x - 100)*.25) + 100 if x >= 100 else x for x in dataframe["A percentage stored as float"]]
What it does: It loops through the each column row, checks if value meets our if stement and then does the applies the calculation, if statement is not met, then it returns the original row value.它的作用:它遍历每一列的行,检查值是否满足我们的 if 词干,然后应用计算,如果不满足语句,则返回原始行值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.