2

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"])

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

3 Comments

This solution is for the case where you want the negatives to be processed in case BOTH columns are negative, which I believe is what you are looking for?
Thanks for showing how to add a nested np.where! Much appreciated. And not quite, I have adjusted my question a bit above. I would want the calculation of (*-0.5) to occur to the each individual column (ex. Total-S_%S, etc.). So, there may only be a negative in one of the three columns, or both which are used in the following calculation outlined in the code I started with. Does this make sense? The creating processed columns in this case may be easier by the looks of it
You're welcome. I modified my answer to reflect your added comment. The first option could still work, but I guess it would require more nested conditions, which becomes cumbersome. I would use the added columns if I were you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.