I want to apply a function to each column in the pandas dataframe if a specific column in that dataframe meets a condition. I have a dataframe with 6 columns and 5 rows. The 6th column is the sum of the first 5 columns and if the sum is greater than 1 for a specific row, I want to multiply all the columns in that row with a number (scalar) to ensure that the sum of that row is lower than 1. Below is a simplified dataframe (my original dataframe has 20 columns and 4 million rows).
A B C D E Sum
1 0.004 0.04 0.08 0.6 0.013 0.737
2 0.12 0.25 0.08 0.6 0.014 1.064
3 0.05 0.02 0.08 0.3 0.019 0.469
4 0.08 0.003 0.05 0.1 0.011 0.244
5 0.56 0.04 0.08 0.7 0.016 1.396
I want to multiply each column on the 2nd and 5th rows by a number to be able to make the sum of those columns less than 1.
I tried to apply the following function to the dataframe but apparently, this code applies that function to each value in the dataframe and I also could not figure out how to select the rows whose sum values are greater than 1.
def func(value):
if value > 1:
return(value * 0.71)
else:
return(value)