Home > front end >  Using case when statements in Python
Using case when statements in Python

Time:09-24

Hi so I come from more of a a sql background, and I'm having hard time using what ever would be the equivalent of a case when statement. I have a column site visits which has range of 0-1000. I want to break it down into 0-299 = small, 300-599 = medium, 600-1000 = large. Here is what I have

df['company_size'] = df['site_visits'].apply(lambda x: 'Small' if (x < 300) else 'Large')

How do I add more statements? I am using pandas software.

CodePudding user response:

Define a function to make it readable if there are many cases.

def size_name(size):
    if size < 300:
        return 'Small'
    if size < 600:
        return 'Medium'
    return 'Large'

df['company_size'] = df['site_visits'].apply(size_name)

CodePudding user response:

Try define a function for your case. As it will be clearer.

Sample Code:

def my_condition(x):
    if x < 300:
        return "Small"
    elif x == 300:
        return "Medium"
    else
        return "Large"
    
df['company_size'] = df['site_visits'].apply(my_condition)

CodePudding user response:

Add an if in else:

df['company_size'] = df['site_visits'].apply(lambda x: 'Small' if (x < 300)
                                             else ('Medium' if x < 600 else 'Large'))

Best with pd.cut:

df['company_size'] = pd.cut(df['site_visits'], 
                            [0, 300, 600, 1000], 
                            labels=['Small', 'Medium', 'Large'])

CodePudding user response:

You can have multiple if conditions in a ternary in python. For example, you could do: (lambda x: 'Small' if (x < 300) else 'Medium' if (x < 600) else 'Large')

  • Related