Home > front end >  how to get column names in pandas of getdummies
how to get column names in pandas of getdummies

Time:04-24

After i created a data frame and make the function get_dummies on my dataframe:

df_final=pd.get_dummies(df,columns=['type'])

I got the new columns that I want and everything is working.

My question is, how can I get the new columns names of the get dummies? my dataframe is dynamic so I can't call is staticly, I wish to save all the new columns names on List.

CodePudding user response:

An option would be:

df_dummy = pd.get_dummies(df, columns=target_cols)
df_dummy.columns.difference(df.columns).tolist()

where df is your original dataframe, df_dummy the output from pd.get_dummies, and target_cols your list of columns to get the dummies.

  • Related