Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

I have the function and want to create a new column df['growth_factor'] which will have a derived value in it. The tricky part is that two other columns need to be passed in a function for every row.

import pandas as pd

df = pd.DataFrame({"city":["losangeles", "losangeles", "newyork"],

                   "beds":[3, 4, 4]})

def growth_factor(city,beds):

    if beds==3:

        if city == 'losangeles'      : return 45

        else: False

    elif beds==4:

        if city == 'losangeles'      : return 47

        elif city == 'newyork'       : return 50

        else: False

    else: False

The function should pass into a df and should look like this:

df=

'city'      | 'beds' | 'growth_factor'

losangeles  |3       | 45

losangeles  |4       | 47

newyork     |4       | 50

How can I achieve this?

1 Answer

0 votes
by (36.8k points)

Use np.select:

import numpy as np

choices=[45,47,50]

conditions=[(df['beds'].eq(3) & df['city'].eq('losangeles')),

            (df['beds'].eq(4) & df['city'].eq('losangeles')),

            (df['beds'].eq(4) & df['city'].eq('newyork'))]

df['growth_factor']=np.select(conditions, choices, default='False')

Output:

df

         city beds growth_factor

0 losangeles 3 45

1 losangeles 4 47

2 newyork 3 False

 If you are a beginner and want to know more about Data Science the do check out the Data Science course

Browse Categories

...