Let's consider this table below:
Mobile | Battery(mAh) | RAM(GB) | Storage(GB) | ED from Ideal best | ED from Ideal worst |
---|---|---|---|---|---|
Sansung | 1000 | 4 | 2 | ||
iPhone | 8000 | 6 | 3 | ||
Motorola | 3000 | 3 | 1 |
Formula for ED from Ideal best = √(( Battery(mAh)- Max of Battery(mAh))^2 (RAM(GB) - Max of RAM(GB) )^2 ( Storage(GB) - Max of Storage(GB))^2)
Formula for ED from Ideal Worst = √(( Battery(mAh)- Min of Battery(mAh))^2 (RAM(GB) - Minof RAM(GB) )^2 ( Storage(GB) - Min of Storage(GB))^2)
For instance, value for Samsung mobile should be √((1000-8000)^2 (4-6)^2 (2-3)^2) = 7000.00035714
Can Anyone help me to write a function in Python where these two formulas can apply for each rows for ED from Ideal best and ED from Ideal worst?
So far I've tried this:
df["Euclidean Distance from ideal best"]= np.sqrt((df["Battery(mAh)"]-df["Battery(mAh)"].max()).pow(2) (df["RAM(GB)"]-df["RAM(GB)"].max()).pow(2) (df["Storage(GB)"]-df["Storage(GB)"].max()).pow(2))
CodePudding user response:
It works fine by me. Just change the typo df1
->df
:
df["Euclidean Distance from ideal best"] = np.sqrt(
(df["Battery(mAh)"] - df["Battery(mAh)"].max())**2
(df["RAM(GB)"]-df["RAM(GB)"].max())**2
(df["Storage(GB)"]-df["Storage(GB)"].max())**2)
CodePudding user response:
The code you've written should work fine (just write df
instead of df1
, which is undefined):
df["Euclidean Distance from ideal best"] = np.sqrt(
(df["Battery(mAh)"] - df["Battery(mAh)"].max()).pow(2)
(df1["RAM(GB)"] - df["RAM(GB)"].max()).pow(2)
(df1["Storage(GB)"] - df["Storage(GB)"].max()).pow(2)
)
you'll get
>>> df
Mobile ... Euclidean Distance from ideal best
0 Sansung ... 7000.000357
1 iPhone ... 0.000000
2 Motorola ... 5000.001300
which matches your expected output
For the other column:
df["Euclidean Distance from ideal worst"] = np.sqrt(
(df["Battery(mAh)"] - df["Battery(mAh)"].min()).pow(2)
(df1["RAM(GB)"] - df["RAM(GB)"].min()).pow(2)
(df1["Storage(GB)"] - df["Storage(GB)"].min()).pow(2)
)