Reputation: 327
Hello i have this dataset
Month Value
Jan 15
Feb 2
Mar 8
Apr 4
May 9
How to achieve to have a new dataframe with the multiplication of each pair of the values of months?
For example something like this
Jan Feb Mar Apr May
Jan - 30 120 60 135
Feb 30 - 16 8 18
Mar 120 16 - 32 72
Apr 60 8 32 - 36
May 135 18 72 36 -
Upvotes: 1
Views: 154
Reputation: 4041
Try matrix multiplication:
df.set_index('Month', inplace=True)
df_product = df @ df.T
# Fill nan in diagonal
df_product = df_product + np.where(np.eye(len(df_product)), np.nan, 0)
Upvotes: 1
Reputation: 75150
You can use a dot product with a transposed dataframe after setting Month as index, Then use a mask to ignore the matching index and columns:
u = df.set_index("Month")
v = u.dot(u.T)
m = list(v.columns)==v.index.to_numpy()[:,None]
out = v.mask(m)#.rename_axis(columns=None)
print(out)
Month Jan Feb Mar Apr May
Month
Jan NaN 30.0 120.0 60.0 135.0
Feb 30.0 NaN 16.0 8.0 18.0
Mar 120.0 16.0 NaN 32.0 72.0
Apr 60.0 8.0 32.0 NaN 36.0
May 135.0 18.0 72.0 36.0 NaN
Upvotes: 2