Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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()):
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading