In Python I have dataframe df and code that goes like that: df…
- User
In Python I have dataframe df and code that goes like that: df c=pd.get_dummies(df[varsc])
c are newly created dummy variables. I want to replace those columns varsc in df with encoded variables and only for binary variables remove the dominant column (with greater number of 1 in in).
- Assistant
To achieve this, you can follow these steps:
- Loop through the
varsclist. - Create dummy variables for each column in
varscusingpd.get_dummies(). - Drop the original column from
df. - Add the new dummy variables to
df. - For binary variables, remove the column with the greater number of 1s.
Here's the code:
import pandas as pd # Replace the columns with dummy variables for col in varsc: dummies = pd.get_dummies(df[col], prefix=col) # Remove the dominant column if it's a binary variable if dummies.shape[1] == 2: dominant_col = dummies.sum(axis=0).idxmax() dummies = dummies.drop(columns=[dominant_col]) # Drop the original column and add the new dummy variables df = df.drop(columns=[col]) df = pd.concat([df, dummies], axis=1)This will replace the columns in
dfwith the dummy variables and remove the dominant column for binary variables. - Loop through the
- User
Just one little thing, I need to replace create new variable named varsc_ce that containts the list of newly created columns . I need to later use it like this: vars_target_id_ce = varsn + varsc_ce + [target_name] + [time_name] + [intercept_name] + [event_value] + [all_value] + id_vars vars_ce = varsn + varsc_ce
Which applies to the code used: enc = ce.BinaryEncoder(cols=varsc) df_ce = enc.fit_transform(df[varsc]) varsc_ce = list(df_ce)
df_ce = enc.fit_transform(df)
df_ce=df
that I do NOT want to use, I want to use code you just written above instead .
- Assistant
You can create the new variable
varsc_cein the code as well. Here's the updated code that creates thevarsc_celist and fills it with the newly created column names:import pandas as pd varsc_ce = [] # Initialize the list to store the new column names # Replace the columns with dummy variables for col in varsc: dummies = pd.get_dummies(df[col], prefix=col) # Remove the dominant column if it's a binary variable if dummies.shape[1] == 2: dominant_col = dummies.sum(axis=0).idxmax() dummies = dummies.drop(columns=[dominant_col]) # Drop the original column and add the new dummy variables df = df.drop(columns=[col]) df = pd.concat([df, dummies], axis=1) # Add the new column names to varsc_ce varsc_ce.extend(dummies.columns) # Now you can use varsc_ce to create vars_target_id_ce and vars_ce vars_target_id_ce = varsn + varsc_ce + [target_name] + [time_name] + [intercept_name] + [event_value] + [all_value] + id_vars vars_ce = varsn + varsc_ceThis code will replace the columns in
dfwith the dummy variables, remove the dominant column for binary variables, and generate the new variable with the list of newly created column names.