'Python groupby by using lambda

I am trying to group some people by lambda function (this is a bank-additional-full dataset)

here is what I am trying to do

df['job_group'] = df['job'].apply( lambda x : 'Jobless' if x == 'retired' else 'Currently Working'

)

So, I am trying to group by Jobless : unemployed, retired, student Currently Working : admin, services, technician ..etc

but if I try like this

df['job_group'] = df['job'].apply( lambda x : 'Jobless' if x == 'retired','student','umemployed' else 'Currently Working'

it does not work

I know this might look silly, but I know almost nothing about it. I would greatly appreciate if you can save my life for now...!!

)



Solution 1:[1]

This could be done with List comprehension.

df['job_status'] = ['Jobless' if x in ['retired','student','unemployed'] else 'Currently working' for x in df['job']]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Praveen