in Education by
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? Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
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

Related questions

0 votes
    I have the dataset that has the no_employees column that is the str object. whats is a best way to create the new ... 1-5 |Very Small Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I have 2 data frames df1 Name 2010 2011 0 Jack 25 35 1 Jill 15 20 df2 Name 2010 2011 0 Berry 45 25 1 ... used the code df1.add(df2) Select the correct answer from above options...
asked Jan 18, 2022 in Education by JackTerrance
0 votes
    I have data like this: id a b c d 1 y y z z 2 y z y y 3 y y y y I want to count the value of "y" ... 1 2 2 1 3 4 Can anyone help me? Select the correct answer from above options...
asked Jan 9, 2022 in Education by JackTerrance
0 votes
    I have a set of oil wells compiled in the panda's data frame. It looks like this: wells = pd.DataFrame({'date ... -01-01 FIELDZ 10.8 Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I have a 20 x 4000 dataframe in python using pandas. Two of these columns are named Year and quarter. I'd ... anyone help with that? Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    I want to calculate a percentage, for each id, of True values from all rows of the id. Here an example ... df.num_true/df.num_col1_id Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I have a dataframe with 2 columns and I want to add the new column; This new column should be updated based on ... +=1 is not working. Select the correct answer from above options...
asked Jan 11, 2022 in Education by JackTerrance
0 votes
    Suppose I have a Tensorflow tensor. How do I get the dimensions (shape) of the tensor as integer values? I ... 'Dimension' instead. Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    To the data frame df: Player Team Points Mean Price Value Gameweek 1 Jim Leeds 4.4 4.40 10.44 0.44 2 Jim ... scalar What am I missing? Select the correct answer from above options...
asked Jan 18, 2022 in Education by JackTerrance
0 votes
    In python, I want to sum all of the values in a variable. My code is x=100 y=200 for i in range(a,b) ... of all numbers which is 7500. Select the correct answer from above options...
asked Jan 11, 2022 in Education by JackTerrance
0 votes
    I have 2 columns in the python dataframe. I want to check each row in my Column A for any value that ... for this particular purpose. Select the correct answer from above options...
asked Jan 9, 2022 in Education by JackTerrance
0 votes
    I am totally new to Machine Learning and I have been working with unsupervised learning technique. Image shows my ... 3 were given Select the correct answer from above options...
asked Feb 1, 2022 in Education by JackTerrance
0 votes
    SA=1000.00 AI=0.12 MP=100.00 def remainb(x): if x==0: return 0 else: return x=(SA+(AI/12)*SA)-MP for ... . run the loop until SA==0). Select the correct answer from above options...
asked Jan 11, 2022 in Education by JackTerrance
0 votes
    I'm starting with input data like this df1 = pandas.DataFrame( { "Name" : ["Alice", "Bob", "Mallory", ... Any hints would be welcome. Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    I am from an SQL background was looking for the best way of doing this. c = a.account_id.isin(b. ... above method efficient way? Select the correct answer from above options...
asked Jan 18, 2022 in Education by JackTerrance
...