0
votes

I am relatively new to Python and I am facing a conditional calculation issue. I have the following table and I would create a new column:

RM1 RM2 RM3 RM1% RM2% RM3% Prod(kg/h) New Column Cotton Prod
Cotton Cotton NaN 50 50 NaN 20 20
Waste Cotton Waste 10 50 40 20 10
Other NaN Cotton 50 NaN 100 20 20

The Problem is that Cotton is not the only raw material within RM1, RM2, RM3 and the percentage of each raw material is given by RM1%, RM2%, RM3%. I would like to calculate only the rows which contain the string "Cotton". Can someone help me with this?

I have something like this:

conditions = [
    (RM1 == "Cotton") & (RM2 != Cotton) & (RM3 != Cotton),
    (RM1 == "Cotton") & (RM2 == Cotton) & (RM3 != Cotton)
    ...all combinations
    ]
    
    values = [RM1% * Prod(kg/h)/100,
    RM1% + RM2 (*Prod(kg/H)]
    
    df['New Columns Cotton Prod'] = np.select(conditions, values)   
Example for the firs row would be: 50% + 50% (*20/100). as these are the two cotton percentages - John Luke
For working and manipulating tabular data I recommend the module pandas. Have a look at it and specify where you are stuck :) - Jacob
I have sosmething like this: conditions = [ (RM1 == "Cotton") & (RM2 != Cotton) & (RM3 != Cotton), (RM1 == "Cotton") & (RM2 == Cotton) & (RM3 != Cotton) ...all combinations ] values = [RM1% * Prod(kg/h)/100, RM1% + RM2 (*Prod(kg/H)] df['New Columns Cotton Prod'] = np.select(conditions, values) - John Luke
If you have code, it should be edited into the question as a minimal reproducible example - Tomerikoo
The title is not a field where you just put in the first line or two of your question -- your title should summarize your actual specific question. How to Ask. If this is a pandas dataframe, please add the pandas tag to your question. It is not clear what you want to do to your dataframe. Do you want new_column = production * sum(RM1% + RM2% + RM3%) but only if the RM values == "Cotton"? Please show your expected output for the given input - Pranav Hosangadi