简体   繁体   English

Pandas - 如果列中的 X 浮点数大于 Y,则找出 X 和 Y 之间的差值并乘以 0.25

[英]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.

相关问题 计算一列中大于x但小于y的元素数 - Count number of elements in a column greater than x but smaller than y x,y = y,y+x 和 x=y , y=x+y 有什么区别? - What is difference between x,y = y,y+x and x=y , y=x+y? 如果 x 或 y 大于/小于 N,则过滤 x,y 坐标 - Filter x,y coordinates if x or y greater/less than N 筛选 pandas dataframe 行,其中具有列 A 和值 X 的特定行具有列 B,其值 Y 大于参数 Z - Filter pandas dataframe rows where a specific row with column A and value X has column B with value Y greater than a parameter Z 删除列值为 X 且时间差小于 Y 的行 - Drop rows with column value X and time difference less than Y from x import y 和 import xy 的区别 - difference between from x import y and import x.y [y代表x.split('_')]和x.split('_')之间的差异 - Difference between [y for y in x.split('_')] and x.split('_') 在python 2.7中,“ x!= y”和“ not x == y”有什么区别? - In python 2.7 what's the difference between “x != y” and “not x == y”? (x不是None)和(y不是None)和(x和y)之间的python区别不是None - python difference between (x is not None) and (y is not None) and (x and y) is not None Python中的“board[x, y]”和“board[x][y]”之间有区别吗? - Is there a difference between `board[x, y]` and `board[x][y]` in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM