Home > Net >  Construct a pandas df from lists, ValueError: too many values to unpack (expected 3)
Construct a pandas df from lists, ValueError: too many values to unpack (expected 3)

Time:03-01

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?

CodePudding user response:

The for does not work as you might expect. consider the following toy example:

for x,y,z in [[1,2,3], [4,5,6], [7,8,9]]:
    print(x,y,z)

you would expect the output to be:

1 4 7
2 5 8
3 6 9

but what you get is

1 2 3
4 5 6
7 8 9

this happans because the loop iterates each item in you list, which is a list on it's own and tries to expand it into your 3 parameters which may or may not exist. to transpose the list (of lists) you can use the built in zip like so

for x,y,z in zip(*[[1,2,3], [4,5,6], [7,8,9]]):
    print(x,y,z)

or in your specific case:

for ind1, ind2, delta in zip(*calculator()):
  • Related