Home > OS >  Python function with for loop and if statement not working as well
Python function with for loop and if statement not working as well

Time:11-02

I have a column with numbers in a csv file and I want to compare them in a function but it won't works well, look below

  def newnumbers(list):
    for x in list:
        if x <= 0.4:
            return 1
        elif x <= 0.5:
            return 2   
        else:
            return 3

     thelist = pd.thecolumn
     pd['newnumbers'] = newnumbers(thelist)

here is what I get

    thecolumn newnumbers
 0  0.021     3
 1  0.003     3
 2  0.323     3
 3  0.576     3
 4  0.567     3
 5  0.687     3

but I want this

    thecolumn newnumbers
 0  0.021     1
 1  0.003     1
 2  0.323     1
 3  0.576     2
 4  0.567     2
 5  0.687     3

CodePudding user response:

Use np.select and (maybe) change your condition <= 0.5 by < 0.6:

import numpy as np

df['newnumbers'] = np.select([df['thecolumn'] <= 0.4,
                              df['thecolumn'] < 0.6],
                              choicelist=[1, 2], default=3)

Output:

>>> df
   thecolumn  newnumbers
0      0.021           1
1      0.003           1
2      0.323           1
3      0.576           2
4      0.567           2
5      0.687           3

CodePudding user response:

It looks like you are using pandas, so use pandas/numpy methods:

import numpy as np
df['newnumbers'] = np.select([df['thecolumn'].le(0.4), df['thecolumn'].le(0.5)],
                             [1,2], 3)

output:

   thecolumn  newnumbers
0      0.021           1
1      0.003           1
2      0.323           1
3      0.576           3
4      0.567           3
5      0.687           3

NB. you condition on the value ≤ 0.5 should have no match, maybe you mean ≤ 0.6?

  • Related