How to build a loop for converting entires of categorical columns to numerical values in Pandas?

Use DataFrame.apply by columns in variable cols:

cols = df.select_dtypes(['category']).columns
df[cols] = df[cols].apply(lambda x: x.factorize()[0])

EDIT:

Your solution should be simplify:

for column in df.select_dtypes(['category']):
    df[column] = df[column].factorize()[0]

I tried the following, which seems to work fine:

for column in df.select_dtypes(['category']):
    df[column] = pd.Series(df[column].factorize()[0])

where 'category' could be 'bool', 'object', etc.