0

I create a function and when i apply it to my dataframe, it returns the error. Could anyone help me with that?

def accompany_alone(passenger):
    sib, parch = passenger 

    if sib >0:
        return 'With Family'
    elif parch >0:
        return 'With Family'
    else:
        return 'Alone'

titanic_df['Alone'] =titanic_df[['SibSp','Parch']].apply(accompany_alone)

Error Image

Dataset

SIGSTACKFAULT
  • 919
  • 1
  • 12
  • 31
Alexia.W
  • 13
  • 3
  • 1
    `sib, parch = passenger` requires exactly two values in `passenger`. If there are more, it will throw the error you described. – John Gordon Oct 27 '18 at 02:26
  • Please post an example DataFrame. - `titanic_df.head()` if there aren't too many columns. – wwii Oct 27 '18 at 04:29
  • Do not post results as screenshots. Format them as text. – jpmc26 Oct 27 '18 at 04:57
  • Possible duplicate of ["Too many values to unpack" Exception](https://stackoverflow.com/questions/1479776/too-many-values-to-unpack-exception) – jpmc26 Oct 27 '18 at 04:58

3 Answers3

0

This kind of error is caused by the mismatch between the number of returned values and the number of variables you are trying to store them into. To be more exact, in your case, the returned values are more than 2 but you are trying to store them in only two variables sib and parch.

I cannot give you an exact answer of how to solve it since you didn't show the exact format of passenger, but here's a short example to show you why the error happens.

Imagine you have the following case:

a = (1,2,3)
c,b = a

This will throw the same error you faced since a has 3 values but we are using only 2 variables to store them. To solve the issue we can do:

a = (1,2,3)
c,b,e = a

Now each value is respectively stored in 1 variable respectively and no error.

Wazaki
  • 899
  • 1
  • 8
  • 22
0

The line sib, parch = passenger assumes that passenger has two elements, which you're trying to assign to sib and parch. The error is saying that two elements were expected (one for sib, one for parch), but that only one was supplied (passenger).

If you're trying to apply accompany_alone() across each row, it could be easier to just iterate over the row indices explicitly, for example something like this would work:

def accompany_alone(sib, arch):
  if sib > 0: return 'With Family'
  elif parch > 0: return 'With Family'
  else: return 'Alone'

titanic_df['Alone'] = [accompany_alone(titanic_df['SibSp'][idx], 
                                       titanic_df['Parch'][idx]) 
                       for idx in range(titanic_df.shape[0])]

Also try playing around with the axis parameter of DataFrame.apply() -- it might not be behaving as you expect (here's a link to the docs).

lefft
  • 2,065
  • 13
  • 20
0

Instead of labels Family and not family, it is preferred to store it as True or False, You can do this,

titanicDatabase['family']=titanicDatabase['Parch'][np.array(titanicDatabase['Parch']>0)| np.array(titanicDatabase['SibSp']>0)]