Search code examples
pythondataframeif-statementconditional-statementsfillna

Column not getting filled in after putting several conditions


Could you tell me what I'm doing wrong. I am using a databases with three columns. I am trying to filter through one of my columns, named 'POT_SALES, using a condition and fill in an empty column, called 'PROFILES', with 5 different profile types.

Here is the code I've been using so far:

for c in df_seg.POT_SALES:
    if c >= 8.0:
        df_seg.PROFILES.fillna('Ambassador')
    elif c >= 6.0:
        df_seg.PROFILES.fillna('High-Potential')
    elif c >= 4.0:
        df_seg.PROFILES.fillna('Starters')
    elif c >= 2.0:
        df_seg.PROFILES.fillna('Competitors')
    else:
        df_seg.PROFILES.fillna('Haters')

After I run it in Jupyter, the data frame doesn't change at all. What do you recommend me to do?

I was trying to fill empty columns, after filtering the data, but nothing happened


Solution

  • here is one approach, using cut

    # using cut, BIN the values per the IF criteria
    # -1 and 99 are just the lower and upper bounds
    # labels to assign when value falls within the corresponding bins
    
    df['profile']=pd.cut(df['POT_SALES'], 
                         bins=[-1,2, 4, 6, 8,99],
                         labels=[ 'Haters','Competitors','Starters','High-Potential','Ambassador'],
                         include_lowest=False,
                         precision=0,
                         right=False,
                         ordered=True,
          )
    df
    
        POT_SALES   profile
    0           0   Haters
    1           1   Haters
    2           2   Competitors
    3           3   Competitors
    4           4   Starters
    5           5   Starters
    6           6   High-Potential
    7           7   High-Potential
    8           8   Ambassador
    9           9   Ambassador