I have the following dataframe.
df = pd.DataFrame([['a', 4], ['b', 1], ['c', 2], ['d', 0], ], columns=['item', 'value'])
df
item | value
a | 4
b | 1
c | 2
d | 0
I want to calculate the pairwise absolute difference between each possible pair of item to give the following output.
item| a | b | c | d
a | 0.0 | 3.0 | 2.0 | 4.0
b | 3.0 | 0.0 | 1.0 | 1.0
c | 2.0 | 1.0 | 0.0 | 2.0
d | 4.0 | 1.0 | 2.0 | 0.0
After a lot of search, I could find answer only to direct element by element difference, which results in a single column output.
So far, I've tried
pd.pivot_table(df, values='value', index='item', columns='item', aggfunc=np.diff)
but this doesn't work.