Home > Back-end >  how to use two columns as condition to create a new column in the data frame which will return a Boo
how to use two columns as condition to create a new column in the data frame which will return a Boo

Time:07-01

I have an idea but i cant seem to write the right code for the problem io want to add a column in the data frame that will return a Boolean value when two conditions are met. the first condition is a multi-condition type i have to find out all adults with higher education (Bachelors or Masters or Doctorate) the second condition is a single condition all adults who have a salary that is greater than 50K when this two conditions are met i want the new column to return true if not then it should return false

CodePudding user response:

It would have been better if you provided a peak of the data but anyways I can understand the scenario in some way. Here is the solution as per my understanding:

df = pd.DataFrame({'Education' : ['bachelor', 'master', 'doc', 'school', 'highschool'],
              'Salary' : [100000, 51000, 40000, 30000, 25000]})

SALARY_THRESH = 50000
req_education =  ['bachelor', 'master', 'doc']

df.apply(lambda x: x['Education'] in req_education and x['Salary'] > SALARY_THRESH,
        axis=1)
  • Related