Using if/else in pandas series to create new series based on conditions
问题 I have a pandas df. Say I have a column "activity" which can be "fun" or "work" and I want to convert it to an integer. What I do is: df["activity_id"] = 1*(df["activity"]=="fun") + 2*(df["activity"]=="work") This works, since I do not know how to put an if/else in there (and if you have 10 activities it can get complicated). However, say I now have the opposite problem, and I want to convert from an id to a string, I cannot use this trick anymore because I cannot multiply a string with a