0

I am trying to fill in my data, first, I used the np.where method

df['temp']=np.where(df['nutrition-score-fr_100g'] <=-1,df['nutrition-score-fr_100g'],  'a' )
df['temp']=np.where((df['nutrition-score-fr_100g']>= 0) & (df['nutrition-score-fr_100g']<=2),df['nutrition-score-fr_100g'], 'b')
df['temp']=np.where((df['nutrition-score-fr_100g']>= 3) & (df['nutrition-score-fr_100g']<=10),df['nutrition-score-fr_100g'], 'c')
df['temp']=np.where((df['nutrition-score-fr_100g']>= 11) & (df['nutrition-score-fr_100g']<=18),df['nutrition-score-fr_100g'], 'd')
df['temp']=np.where((df['nutrition-score-fr_100g']>= 19) & (df['nutrition-score-fr_100g']<=40),df['nutrition-score-fr_100g'], 'e')

but unfortunately all df['temp'] values were equal to 'e'

I have tried another method to fill my data and this time I've used a for loop and again unfortunately the same problem persists, all my df['temp'] were equal to 'c' this time

for i in range (0, len(df)): #sans else, affiche la meme valeurs partout
  if df['nutrition-score-fr_100g'][i] <-1:
    df['temp']='a'
  elif df['nutrition-score-fr_100g'][i] in range (0, 3):
    df['temp']='b'
  elif df['nutrition-score-fr_100g'][i] in range (3, 11):
    df['temp']='c'
  elif df['nutrition-score-fr_100g'][i] in range (11, 19):
    df['temp']='d'
  elif df['nutrition-score-fr_100g'][i] in range (19, 41):
    df['temp']='e'

please any help? many thanks

  • Could you please provide a minimum sample of the df data with the nutrition-score column. – user19077881 Dec 14 '22 at 13:14
  • the first five values [ 9, 14, 0, 12, 9] – islem habibi Dec 14 '22 at 13:34
  • comment that won't fix everything but to reproduce your logic `if df['nutrition-score-fr_100g'][i] <-1: df['temp']='a'` then the values to choose in `np.where` should be switched: `np.where(df['nutrition-score-fr_100g'] <=-1, 'a',df['nutrition-score-fr_100g'] )` and same for the rest. That said, to solve your problem, you should better look at `np.select`, see [this QA](https://stackoverflow.com/questions/30631841/how-do-i-assign-values-based-on-multiple-conditions-for-existing-columns) for example – Ben.T Dec 14 '22 at 13:46
  • See [ask]. Debug yourself at first. Did you log ```df['temp']``` at each step? You are overwriting the whole column at each step. – relent95 Dec 14 '22 at 14:34

1 Answers1

1

Your logic has gone awry. It is better to use pandas .where as below because the logic is simpler; note the ~ character just changes the where condition to True. Hope this helps.

import pandas as pd

df = pd.DataFrame({'nutrition-score-fr_100g': [9, 14, 0, 12, 9] })

df['temp'] = 'X'   # default value
df['temp'].where(~df['nutrition-score-fr_100g'] <=-1,  'nutrition-score-fr_100g' , inplace = True)
df['temp'].where(~(df['nutrition-score-fr_100g']>= 0) & (df['nutrition-score-fr_100g']<=2), 'b', inplace = True)
df['temp'].where(~(df['nutrition-score-fr_100g']>= 3) & (df['nutrition-score-fr_100g']<=10), 'c', inplace = True)
df['temp'].where(~(df['nutrition-score-fr_100g']>= 11) & (df['nutrition-score-fr_100g']<=18), 'd', inplace = True)
df['temp'].where(~(df['nutrition-score-fr_100g']>= 19) & (df['nutrition-score-fr_100g']<=40), 'e', inplace = True)

print(df)

which outputs:

0   1    b
1   9    c
2  14    d
3   0    b
4  12    d
5   9    c
user19077881
  • 3,643
  • 2
  • 3
  • 14