pandas modify DataFrame column name

Keywords: Python Lambda

This article is from: pandas modify DataFrame column name
The original blog does the same modification for each element in DataFrame.columns
But my work is a rote to do different operations for each element. Please give me your advice

Raise questions

There is a DataFrame named dataset

>>> dataset.columns
Index(['age', 'job', 'marital', 'education', 'default', 'housing', 'loan',
       'contact', 'month', 'day_of_week', 'duration', 'campaign', 'pdays',
       'previous', 'poutcome', 'emp.var.rate', 'cons.price.idx',
       'cons.conf.idx', 'euribor3m', 'nr.employed', 'y'],
      dtype='object')

Now, I want to change its columns name to:

>>> new_columns
Index(['age_0', 'job_1', 'marital_2', 'education_3', 'default_4', 'housing_5',
       'loan_6', 'contact_7', 'month_8', 'day_of_week_9', 'duration_10',
       'campaign_11', 'pdays_12', 'previous_13', 'poutcome_14',
       'emp.var.rate_15', 'cons.price.idx_16', 'cons.conf.idx_17',
       'euribor3m_18', 'nr.employed_19', 'y_20'],
      dtype='object')

How to operate?

Solve

1. Modify by the DataFrame.columns class's own properties:

1. Direct modification without brain assignment

>>> # First, solve the derivation of 'new columns'
>>> # List inference
>>> new_columns_list = [column_str+'_'+str(i) for i ,column_str in enumerate(dataset.columns)]
>>> # Type conversion
>>> new_columns = pd.core.indexes.base.Index(new_columns_list)
>>> dataset.columns = new_columns

2. Modify through the. map(mapper, na_action=None) function

>>> # Note: mapper often uses lambda expression
>>> # But I don't seem to find a way to change two values in a lambda expression
>>> # So we can only use a global variable i and map function mapper()
>>> # I hope you can help me find a way

>>> i = 0
>>> def mapper(x): # Mapping function is mapper
    global i
    x += '_' + str(i)
    i += 1
    return x
>>> dataset.columns.map(mapper)

3. Reference blog uses DataFrame.columns.str object

Use help(DataFrame.columns.str) to browse through the document,
I couldn't find a way that I could apply. I wanted to take the time to translate this document

2. Modify by DataFrame.rename() function

1. Violence Dictionary (advantage: only specific columns can be modified)

>>> # First, we use dictionary derivation
>>> new_dict = {
    key:key+'_'+str(i)
    for i, key in enumerate(dataset.columns)
    }
>>> dataset.rename(columns=new_dict, inplace=True)

2. Mapping modification method

>>> # The original blog still uses lambda expression
>>> # I'll move the hard cover again and copy the above
>>> # Using a global variable i and mapping function mapper()

>>> i = 0
>>> def mapper(x): # Mapping function is mapper
    global i
    x += '_' + str(i)
    i += 1
    return x
dataset.rename(columns=mapper, inplace=True)

Written in the end

You are welcome to modify my work or make suggestions directly!!!

Posted by mahendrakalkura on Sat, 30 Nov 2019 12:56:39 -0800