jeudi 16 juin 2016

Making python lambda func work inside apply method in Pandas Dataframe


I am trying to transform the age columns of a pnada dataframe by applying apply function. how to make below code work or is there a more pythonic way way to do this.

cps=(cps.assign(Age_grp_T=cps['age'].apply(lambda x:{x>=71:'Tradionalists',
                                                  71>x>=52:'Baby Boomers',
                                                  52>x>=46:'Generation X',
                                                  46>x>=16:'Millennials'}.get(x, ' ')))

Aucun commentaire:

Enregistrer un commentaire