python: ValueError: too many values to unpack (expected 2) data from excell
Asked Answered
D

3

13

I want to take data from excel and plot 2D kernel density estimate in python, but it says "ValueError: too many values to unpack (expected 2)". how to fix it? following the coding:

# libraries
import matplotlib.pyplot as plt
from scipy.stats import kde
import pandas as pd
 
# create data
x = pd.read_excel(r'C:\Users\Ezra\Desktop\montex.xlsx')
y = pd.read_excel(r'C:\Users\Ezra\Desktop\montey.xlsx')
 
# Evaluate a gaussian kde on a regular grid of nbins x nbins over data extents
nbins=500
k = kde.gaussian_kde([x,y])
xi, yi = pd.mgrid[x.min():x.max():nbins*100j, y.min():y.max():nbins*100j]
zi = k(pd.vstack([xi.flatten(), yi.flatten()]))
 
# Make the plot
plt.pcolormesh(xi, yi, zi.reshape(xi.shape), shading='auto')
plt.show()
 
# Change color palette
plt.pcolormesh(xi, yi, zi.reshape(xi.shape), shading='auto', cmap=plt.cm.Greens_r)
plt.show()
Diena answered 9/5, 2021 at 13:10 Comment(0)
S
12

When you're getting an error from your code, it would help to pose the actual traceback, especially the part that indicates which line of your sample code is causing the error.

When you call a function that returns multiple values, you can "unpack" it into individual variables. ValueError: too many values to unpack (expected 2) means that you called a function that only returns a single value, but you tried to unpack the return value into two variables.

For example, consider this little python script:

def returns_1_val(): 
    return 'one'

def returns_2_vals(): 
    return 'one', 'two'

print(returns_2_vals())

# Unpack the return value.
x,y = returns_2_vals()
print('x', x)
print('y', y)

print(returns_1_val())

# This next call fails.  We're "expecting" Python to unpack 2 values into 
# x and y, but it fails because the function only returned one value.   
x,y = returns_1_val()

When you run it:

('one', 'two')
x one
y two
one
Traceback (most recent call last):
  File "unpack_err.py", line 11, in <module>
    x,y = returns_1_val()
ValueError: too many values to unpack (expected 2)

The more general error message makes it a little more clear. For example, if you try to call x,y,z = return_2_vals(), you'll get

ValueError: not enough values to unpack (expected 3, got 2)

Saimon answered 9/5, 2021 at 13:30 Comment(0)
A
10

I dont think the answer of Tom Bryan is correct. Your error says: ""ValueError: too many values to unpack (expected 2)" which is the opposite example given by Tom.

Tom says "ValueError: too many values to unpack (expected 2) means that you called a function that only returns a single value". I think this is wrong, what it means is that you called a function that returns more than 2 values. Your function is probably trying to assign more than 2 values to only 2 variables.

Ankh answered 14/11, 2022 at 22:51 Comment(0)
H
0

Try passing 'x' and 'y' as separate arguments to the gaussian_kde function. 'x' and 'y' from the dataframe are the same. Let the arguments be different

Hesitation answered 10/8, 2023 at 13:41 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Dashiell

© 2022 - 2024 — McMap. All rights reserved.