-3

So I'm new to coding and working my way through the Titanic data set from Kaggle. In the data frame there are columns labeled "Age" and "Sex." I want to make a new column that has 4 values that indicate for different things: 0: Male aged 16 or over 1: Female aged 16 or over 2: Male aged 15 or younger 3: Female aged 15 or younger

So far I've gotten this, but this just splits it up into 3 values, not the 4 I'm looking for.

def person_detail(Person):
    Age, Sex = Person
    return 2 if Age < 16 else Sex

I then apply the function to the "Age" and "Sex" columns to get a new column. I know that you can't have two returns, but if it gets across what I am trying to accomplish, it would look something like the below.

def person_detail(Person):
    Age, Sex = Person
    return 2 if Age < 16 and Sex == 0
    return 3 if Age < 16 and Sex == 1 else Sex

Thanks in advance. For reference in the "Sex" column 0 is for male, and 1 is for female.

giantsrock28
  • 85
  • 1
  • 5
  • @NotBad4U - That answers the title of this question, but, unfortunately, that title seems to have nothing to do with the actual question, so it's not a duplicate. This question is basically "how do I use `elif` statements?". – TigerhawkT3 Jul 30 '16 at 22:44

3 Answers3

2

Just use a if...else statement. I guess what you want is:

def person_detail(Person):
    Age, Sex = Person
    if Age<16:
        return 3 if Sex else 2
    else:
        return Sex
miindlek
  • 3,387
  • 11
  • 24
  • Alternatively, nested ternary expression like `return (3 if Sex else 2) if Age < 16 else Sex` but I think the "long" `if/else` is actually more readable. – tobias_k Jul 30 '16 at 22:18
  • Thank you very much, I guess I'm being thick, but could you explain "return 3 if Sex else 2"? Essentially, how does that code get across that I want 3 for under 16 females and 2 for under 16 males? – giantsrock28 Jul 30 '16 at 23:27
0

You could use multiple if statements, an if-else statement or a dictionary:

d = {(0, 0): 0, (0, 1): 1, (1, 0): 2, (1, 1): 3}

def person_detail(Person):
    Age, Sex = Person
    return d[Age < 16, Sex]
Domme
  • 111
  • 5
  • 1
    This might make the code shorter, but not more readable. – tobias_k Jul 30 '16 at 22:16
  • I just wanted to introduce a method to the OP which can be used for similar problems with more possible outcomes (when using more and more if-then-else statements would become a mess). – Domme Jul 30 '16 at 22:25
  • And that's fine, but at least in it's current form, it's hard to understand. I'd suggest moving the dict into the function, giving it a proper name, and making clear that the first part is a boolean: `outcomes = {(False, 1): 0, ...}` – tobias_k Jul 30 '16 at 22:32
0

In my opinion, you should just use a "long" if/else as suggested by @miindlek to make the different outcomes of the function clear. Also, I would suggest not just to return Sex but to return 1 if Sex else 0, again to make exactly clear what the function returns.

But if you prefer to have a very short return line, you could also use a ternary statement like so:

return Sex + 2 if Age < 16 else Sex

Or even shorter (but not easier to understand):

return (Age < 16) * 2 + Sex
tobias_k
  • 74,298
  • 11
  • 102
  • 155