convert categorical variable to numeric python

# get all categorical columns in the dataframe
catCols = [col for col in df1.columns if df1[col].dtype=="O"]

from sklearn.preprocessing import LabelEncoder

lb_make = LabelEncoder()

for item in catCols:
    df1[item] = lb_make.fit_transform(df1[item])

Here is what the above code is Doing:
1. We are creating a list of all the categorical columns in the dataframe.
2. We are creating an instance of the LabelEncoder class.
3. We are using a for loop to iterate over all the categorical columns and assigning the numerical values to them.