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)
pandas
dataframe, please add the pandas tag to your question. It is not clear what you want to do to your dataframe. Do you wantnew_column = production * sum(RM1% + RM2% + RM3%)
but only if the RM values== "Cotton"
? Please show your expected output for the given input - Pranav Hosangadi