Search code examples
pythonpandasdummy-variable

2 dummy variables in a dataframe


I have a dataframe x_0 of 5 numeric variables and 2 categorical variables: ['v1', 'v2', 'v3', 'v4', 'v5', 'v6', 'v7']

I want to convert v6 and v7 to dummy variables. I have tried with the following code but I receive a traceback:

x = pd.concat([x_0['v1', 'v2', 'v3', 'v4', 'v5'], pd.get_dummies(x_0['v6', 'v7'])], axis = 1)

Solution

  • Use double [] for selecting by multiple columns names:

    x = pd.concat([x_0[['v1', 'v2', 'v3', 'v4', 'v5']], 
                   pd.get_dummies(x_0[['v6', 'v7']])], axis = 1)