How to assign unique group-ids when using groupby on multiple columns?

The code below does exactly as intended, which is, to group rows which share identical values in 3 columns (col_A, col_B and col_C) while having a different value for another column (col_Z):

df.groupby(["col_A", "col_B", "col_C"])['col_Z'].nunique().to_frame(name='grouping_count')

However, I have no idea how assign a unique ID to each of these groups. I tried the code below, which is something you would find on Stackoverflow, however, it only seems to work when groupby is used on only 1 column, as it results in a keyerror when multiple columns are used (but not when one column is used):

df.groupby(["col_A", "col_B", "col_C"])['col_Z'].ngroup().to_frame("groupID")

What I expected to happen:
I would expect this to assign a unique numerical ID to each group, in a new column named “groupID”.

What actually happened:
resulted in a keyerror

KeyError: 'col_A'

I found a partial solution myself, based on a Stackoverflow question, the code for which is stated below. It is a partial solution, because it does not take “col_Z” into account.

df.assign(id=(df['col_A'] + '_' + df['col_B'] + '_' + df['col_C']).astype('category')