samedi 26 mai 2018

how to use elif with pandas dataframe

I have a sales dataframe which looks like below

    Company         Sales
    MC                360.0
    MC                340.0
    MC                338.5
    MC                335.5
    MC                235.0
    MC                235.0
    MC                234.0
    MC                127.0
    MC                121.0
    MC                120.5

I want to create a new column based on the values of Sales column And my output table should be like

    CompanyCode     ActivityDate    Category
    MC                360.0         Fast Mover
    MC                340.0         Fast Mover
    MC                338.5         Fast Mover
    MC                335.5         Fast Mover
    MC                235.0         Medium Fast Mover
    MC                235.0         Medium Fast Mover
    MC                234.0         Medium Fast Mover
    MC                127.0         Slow Mover
    MC                121.0         Slow Mover
    MC                120.5         Slow Mover

I tried elif statement

if df['Sales']>=300:
   df['Category'] = 'Fast Movers'
elif (df['Sales']>=200) & (df['Sales'] < 300) :
   df['Category'] = 'Medium Fast Movers'
else:
   df['Category'] = 'Slow Movers'

Not sure if my approach is correct. I will appreciate your kind help and effort.

Aucun commentaire:

Enregistrer un commentaire