0

I am creating a new column with "if, elif, else" based on another column.

My syntax is generating only the value listed in my "if" statement, however it should be returning "elif" and "else" for some values in the other column.

The variable I am creating is OHX02N, based on OHX02CTC, and I am getting 3 for all, which is the command in my first if statement, even though it should be returning 3,1,1,3,1 for the first five obs.

Thoughts?

def F (value):
    if FINALSET.loc[value,'OHX02CTC'] == 'E' or 'P' or 'R':
        FINALSET.loc[value, 'OHX02N'] = 3
    elif FINALSET.loc[value,'OHXO2CTC'] == 'J' or 'T':
        FINALSET.loc[value,'OHX02N'] = 2
    elif FINALSET.loc[value,'OHX02CTC'] == 'K' or 'Z':
        FINALSET.loc[value,'OHX02N'] = 1
    else:
        FINALSET.loc[value,'OHXO2N'] = 0

for value in range(len(FINALSET)):
    F(value)
  • i dont really understand the question you are asking Estelle. – Fallenreaper Jun 19 '18 at 02:19
  • as @thatotherguy points out: the problem is your grouping. You imagine `if THIS == THAT or THE_OTHER` to do something it doesn't do. What it does instead is: `if (THIS == THAT) or (THE_OTHER)`. Since your "other" is just the string `'P'`, which is always truthy, it always works. – Adam Smith Jun 19 '18 at 02:21
  • Other guy I am not trying to make a list I am trying to make a column. So the question you referenced doesn't answer mine. – Rogue_Epidemiologist Jun 19 '18 at 04:04
  • Okay so here's what I want to do: – Rogue_Epidemiologist Jun 19 '18 at 04:05
  • Sorry new on here, enter adds comment ok. I would like to create a new column, one that consists of integers, called OHX02N. This column is based on the character values in the column OHX02CTC, which can be 'D' or 'E' or 'F'... based on these characters (and depending on what the character string is I would like integers. – Rogue_Epidemiologist Jun 19 '18 at 04:07
  • In my code however, it kind of works but it always sets my new column equal to '3' which is my command in the first statement, and seems to ignore the rest. – Rogue_Epidemiologist Jun 19 '18 at 04:07
  • How do I turn off the marked as duplicate because this is definitely not duplicated from the linked question above because Column != List – Rogue_Epidemiologist Jun 19 '18 at 04:14

0 Answers0