Home > Enterprise >  Pandas - if X float in column is greater than Y, find difference between X and Y and multiply by .25
Pandas - if X float in column is greater than Y, find difference between X and Y and multiply by .25

Time:10-19

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. 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.)

The data looks something like:

Some columns here A percentage stored as float
foobar 84.85
foobar 15.95
fuubahr 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
foobar 15.95
fuubahr 100.5625

Thanks!

CodePudding user response:

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.

  • Related