I have a pandas df, and would like to calculate all the possible differences between the values of a certain column while retaining the indexes of the rows that generated each difference value.
To my python newbie mind, the most reasonable way to do so appears to be the following:
- create a function that locates all the values taking part in the computation and compute the differences
- have the function return three lists: the two indexes taking part in the operation and the result
- store these three lists in a df, as brilliantly suggested in this other thread. I am using the following code:
ind1 = []
ind2 = []
delta = []
def calculator(): # id stands for index
for i in range(len(df)):
for j in range(len(df)):
v1 = df.loc[i, 'col']
v2 = df.loc[j, 'col']
dv = abs(v1-v2)
delta.append(dv)
ind1.append(i)
ind2.append(j)
return ind1, ind2, delta
The problem arises when constructing the new df, as I get an unpacking problem:
data = []
for ind1, ind2, delta in calculator():
data.append([ind1, ind2, delta])
new_df = pd.DataFrame(data, columns=['ind1', 'ind2', 'delta'])
returns:
ValueError: too many values to unpack (expected 3)
Any idea on how to solve this issue, while constructing the df properly as indicated in the other thread?