For dataframe
In [2]: df = pd.DataFrame({\'Name\': [\'foo\', \'bar\'] * 3, ...: \'Rank\': np.random.randint(0,3,6), ...:
The correct solution is to use grouper.label_info:
grouper.label_info
df["GroupId"] = df.groupby(["Name", "Rank"]).grouper.label_info
It automatically associates each row in the df dataframe to the corresponding group label.
df