'how to get column names in pandas of getdummies

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.



Solution 1:[1]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 CainĂ£ Max Couto-Silva