Issue
I have a data frame:
product | cost | |
---|---|---|
0 | product a | 56 |
1 | product b | 59 |
2 | product c | 104 |
I'd like to make a percentage change matrix like:
product a | product b | product c | |
---|---|---|---|
product a | -5.08% | -46.15% | |
product b | 5.36% | -43.30% | |
product c | 85.71% | 76.27% |
There could be n number of products.
How do I this using pandas?
How do I get the highest / lowest percentage change products? i.e. Highest: product a vs. product c. Lowest: product c vs. product a.
Thank you for your help.
Solution
Use numpy broadcasting:
# convert columns to arrays
idx = df['product'].to_numpy()
cost = df['cost'].to_numpy()
# compute the percentage change using broadcasting
# convert to DataFrame
out = pd.DataFrame(((cost[:,None]-cost)/cost*100).round(2),
index=idx, columns=idx)
# optional, set NaNs in the diagonal
np.fill_diagonal(out.values, np.nan)
print(out)
Output:
product a product b product c
product a NaN -5.08 -46.15
product b 5.36 NaN -43.27
product c 85.71 76.27 NaN
Answered By - mozway
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.