Let's say I have a dataframe of people with columns age ("old" or "young"), gender ("male" or "female"), and education ("no high school", "high school", or "college"). I want to group people into 12 groupings based off of these columns. The following code gets me what I want, but I was wondering if there is a more idiomatic way in Pandas to do this?
i = 1
df['group'] = 0
for a in ['old', 'young']:
for g in ['male', 'female']:
for e in ["no high school", "high school", "college"]:
df.loc[((df.age == a) &
(df.gender == g) &
(df.education == e)), 'group'] = i
i = i + 1