How to rename one-hot encoded columns in pandas to their respective index?

pandas one hot encoding multiple columns
reverse one hot encoding pandas
pandas rename column
one-hot encoding sklearn pandas
rename column by index pandas
pd.get_dummies drop first=true
pandas indicator column
pandas encode categorical

I'm one-hot encoding some categorical variables with some code that was provied to me. This line adds a column of 0s and 1s with a name with the format prefix_categoricalValue

dataframe = pandas.concat([dataframe,pandas.get_dummies(dataframe[0], prefix='protocol')],axis=1).drop([0],axis=1)

I want the column to have as a name its index, not prefix_categoricalValue.

I know that I can do something like df.rename(columns={'prefix_categoricalValue': '0'}, inplace=True), but I'm not sure how to do it for all the columns which have that prefix.

This is an example of a part of the dataframe. Whether I decide to leave the local_address prefix or not, each category will have its name. Is it possible to rename the column with its index?

EDIT:

I'm trying to do this:

for column in dataframe:
    dataframe.rename(columns={column: 'new_name'}, inplace=True)
        print (column)

but I'm not exactly sure why it doesn't work

import pandas as pd

# 'dataframe' is the name of your data frame in the question, so that's what I use
# in my code below, although I suggest using 'data' or something for it instead, 
# as 'DataFrame' is a keyword and its easy to make confusion. But anyway...

features = ['list of column names you want one-hot encoded']
# for example, features = ['Cars', 'Model, 'Year', ... ]

for f in features: 
    df = dataframe[[f]]

    df2 = (pd.get_dummies(df, prefix='', prefix_sep='')
                   .max(level=0, axis=1)
                   .add_prefix(f+' - '))  
    # the new feature names will be "<old_feature_name> - <categorical_value>"
    # for example, "Cars" will get transformed to "Cars - Minivan", "Cars - Truck", etc


    # add the new one-hot encoded column to the dataframe
    dataframe = pd.concat([dataframe, df2], axis=1)

    # you can remove the original columns, if you don't need them anymore (optional)
    dataframe = dataframe.drop([f], axis=1) 

One-Hot Encoding a Feature on a Pandas Dataframe: Examples, Is it possible to rename the column with its index? add the new one-hot encoded column to the dataframe dataframe = pd.concat([dataframe,  The columns can also be renamed by directly assigning a list containing the new names to the columns attribute of the dataframe object for which we want to rename the columns. The disadvantage with this method is that we need to provide new names for all the columns even if want to rename only some of the columns. # Import pandas package.

Let's say your prefix is local_address_0.0.0.0. The following code renames the columns that start with the prefix you specify to the index that column has according to the order in which they appear in the dataframe:

prefix = 'local_address_0.0.0.0'
cols = list(dataframe)
for idx, val in enumerate(cols):
    if val.startswith(prefix):
        dataframe.rename(index=str, columns={val: idx}, inplace=True)

This will show a warning in the console:

python3.6/site-packages/pandas/core/frame.py:3027: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas- 
docs/stable/indexing.html#indexing-view-versus-copy
return super(DataFrame, self).rename(**kwargs)

But it is just a warning, the column names of the dataframe are updated. If you want to learn more about the warning, see How to deal with SettingWithCopyWarning in Pandas?

If someone knows how to do the same thing without a warning, please comment.

pandas.DataFrame.rename, If 'raise', raise a KeyError when a dict-like mapper , index , or columns contains labels that are not present in the Index being transformed. If 'ignore', existing  Rename all the column names in python: Below code will rename all the column names in sequential order # rename all the columns in python df1.columns = ['Customer_unique_id', 'Product_type', 'Province'] first column is renamed as ‘Customer_unique_id’. second column is renamed as ‘Product_type’. third column is renamed as ‘Province’.

IIUC

dummydf=pd.get_dummies(df.A)
dummydf.columns=['A']*dummydf.shape[1]
dummydf
Out[1171]: 
   A  A
0  1  0
1  0  1
2  1  0
df
Out[1172]: 
   A  B  C
0  a  b  1
1  b  a  2
2  a  c  3

pandas.Series.rename, Alter Series index labels or name. Function / dict values must be unique (1-to-1). Labels not contained in a dict / Series will be left as-is  One-hot encoding a column in a Pandas Dataframe. To create a dataset similar to the one used above in Pandas, we could do this: import pandas as pd df = pd.DataFrame({'country': ['russia', 'germany', 'australia','korea','germany']}) original-dataframe.

Predictive Analytics with TensorFlow: Implement deep learning , We don't need to use the timestamps in this example, so let's drop it from the dataframe: initial_buys_df = initial_buys_df.drop('Timestamp', 1) initial_clicks_df isin(top_k)] Now, let's create a copy of the index, since we will also apply one hot encoding on First, we use one hot encoding on all columns for clicks and buys:  Index.rename(self, name, inplace=False) [source] ¶ Alter Index or MultiIndex name. Able to set new names without level. Defaults to returning new index. Length of names must match number of levels in MultiIndex. namelabel or list of labels. Name (s) to set. inplacebool, default False. Modifies the object directly, instead of creating a new

pandas.get_dummies, Add a column to indicate NaNs, if False NaNs are ignored. columnslist-like, default None. Column names in the DataFrame to be encoded. If columns is None then  Pandas is one of those packages and makes importing and analyzing data much easier. Pandas rename() method is used to rename any index, column or row. Renaming of column can also be done by dataframe.columns = [#list]. But in the above case, there isn’t much freedom. Even if one column has to be changed, full column list has to be passed.

Master Python's pandas library with these 100 tricks, Solution: Set the first column as the index (when reading) Alternative: Don't Need to rename all of your columns in the same way? Use a string pandas trick​: Want to dummy encode (or "one hot encode") your DataFrame? Data Science Quick Tips - How to rename a column in Pandas.

Comments
  • can you show us some example ?
  • just don't use prefix at all?! (it's default value is None)
  • @Peyman I would still end up with column names with out the prefixes, for example: category1, category2, ..., categoryN