Dataframe:
product1 | product2 | product3 | product4 | product5 |
---|---|---|---|---|
straws | orange | melon | chair | bread |
melon | milk | book | coffee | cake |
bread | melon | coffe | chair | book |
CountProduct1 | CountProduct2 | CountProduct3 | Countproduct4 | Countproduct5 |
---|---|---|---|---|
1 | 1 | 1 | 1 | 1 |
2 | 1 | 1 | 1 | 1 |
2 | 3 | 2 | 2 | 2 |
RatioProduct1 | RatioProduct2 | RatioProduct3 | Ratioproduct4 | Ratioproduct5 |
---|---|---|---|---|
0.28 | 0.54 | 0.33 | 0.35 | 0.11 |
0.67 | 0.25 | 0.13 | 0.11 | 0.59 |
2.5 | 1.69 | 1.9 | 2.5 | 1.52 |
I want to create five others columns that keep my initial ratio of each item along the dataframe.
Output:
InitialRatio1 | InitialRatio2 | InitialRatio3 | InitialRatio4 | InitialRatio5 |
---|---|---|---|---|
0.28 | 0.54 | 0.33 | 0.35 | 0.11 |
0.33 | 0.25 | 0.13 | 0.31 | 0.59 |
0.11 | 0.33 | 0.31 | 0.35 | 0.13 |
CodePudding user response:
If you're after code to create the init_rateX
columns then the following will work
pd.DataFrame(
np.divide(
df[["ratio1", "ratio2", "ratio3", "ratio4", "ratio5"]].to_numpy(),
df[["Count1", "Count2", "Count3", "Count4", "Count5"]].to_numpy(),
),
columns=["init_rate1", "init_rate2", "init_rate3", "init_rate4", "init_rate5"],
)
which gives
init_rate1 init_rate2 init_rate3 init_rate4 init_rate5
0 0.28 0.25 0.33 0.57 0.835
1 0.33 0.13 0.97 0.65 0.760
2 0.54 0.11 0.45 0.95 1.160
3 0.35 0.59 0.34 1.25 1.650
However it does not agree with your calcs for init_rate4
or init_rate5
so some clarification might be needed.
CodePudding user response:
It turned out only with a cycle. Check it out, what did you expect?
Output
variable ratio1 ratio2 ratio3 ratio4 ratio5
time
1 0.28 0.25 0.33 0.57 0.54
2 0.33 0.13 0.97 0.59 0.34
3 0.54 0.11 0.45 0.57 0.97
4 0.35 0.59 0.34 0.13 0.25