Reputation: 25
So, I currently have the below code- which calculates a new column based on two others, depending on which one has data present. I would like to add an additional step to the code. Where if the data in a column being used in the calculations (Total-S_%S
, Sulphate-S_%S
and Sulphate-S(HCL Leachable)_%S
) is negative, the program will times the negatives value by -0.5, and then continue with the calculations as described in the code already. I'm not really sure where to start with this. Thanks,
df['Sulphide-S(calc)-C_%S'] = np.where(
df["Sulphate-S(HCL Leachable)_%S"].isna(),
df["Total-S_%S"]- df["Sulphate-S_%S"],
df["Total-S_%S"]- df["Sulphate-S(HCL Leachable)_%S"])
Upvotes: 2
Views: 129
Reputation: 194
The first option below doesn't really work after the additional input from this answer's comments, but I will keep it here for reference. Please look at the second option for the actually working solution You could create a nested np.where() statement like this:
df['Sulphide-S(calc)-C_%S'] = np.where((df["Total-S_%S"] < 0) & (df["Sulphate-S(HCL Leachable)_%S"] >= 0),
np.where(
df["Sulphate-S(HCL Leachable)_%S"].isna(),
df["Total-S_%S"]- df["Sulphate-S_%S"],
df["Total-S_%S"]- df["Sulphate-S(HCL Leachable)_%S"]),
np.where(
df["Sulphate-S(HCL Leachable)_%S"].isna(),
df["Total-S_%S"] * (-0.5) - df["Sulphate-S_%S"] * (-0.5),
df["Total-S_%S"] * (-0.5) - df["Sulphate-S(HCL Leachable)_%S"] * (-0.5))
Working solution below
Otherwise, you could create 2 columns, each of which would have processed values from the initital ones, like this:
df["Sulphate-S_%S_PROCESSED"] = np.where(df["Sulphate-S_%S"] < 0,
df["Sulphate-S_%S"] * -0.5,
df["Sulphate-S_%S"])
and then conduct you computations on these new columns
Upvotes: 4