Home >Backend Development >Python Tutorial >Introduction to the method of modifying DataFrame column names in pandas (code example)

Introduction to the method of modifying DataFrame column names in pandas (code example)

不言
不言forward
2019-02-22 14:29:385621browse

This article brings you an introduction to the method of modifying DataFrame column names in pandas (code examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

This article is referenced from: pandas Modify DataFrame column name
The original blog does the same modification operation for each element in DataFrame.columns
But my humble work is a mechanical copy of doing different operations for each element. Please feel free to enlighten me

Ask a question

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 add itcolumnsName changed 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?

Solution

1. Modify through its own properties of the DataFrame.columns class:

1. Direct modification without brain assignment

>>> # 先解决`new_columns`的推导问题
>>> # 列表推导
>>> new_columns_list = [column_str+'_'+str(i) for i ,column_str in enumerate(dataset.columns)]
>>> # 类型转换
>>> new_columns = pd.core.indexes.base.Index(new_columns_list)
>>> dataset.columns = new_columns

2. Use the .map(mapper, na_action=None) function to modify

>>> # 注:mapper 多运用 lambda 表达式
>>> # 但我似乎没有找到在 lambda 表达式中改变两个值的方法
>>> # 所以只能蹩脚地用一个全局变量i, 和映射函数mapper()
>>> # 希望大家能帮我找到方法

>>> i = 0
>>> def mapper(x): # 映射函数即 mapper
    global i
    x += '_' + str(i)
    i += 1
    return x
>>> dataset.columns.map(mapper)

3. Refer to the blog for the DataFrame.columns.strObject

Usedhelp(DataFrame.columns.str)I searched through the documents,
I couldn’t find one that I could use Applying the method, I want to take the time to translate this document

2. Modify it through the DataFrame.rename() function

1. Violent dictionary method (benefits : You can only modify specific columns)

>>> # 此处先用字典推导法
>>> new_dict = {
    key:key+'_'+str(i)
    for i, key in enumerate(dataset.columns)
    }
>>> dataset.rename(columns=new_dict, inplace=True)

2. Mapping modification method

>>> # 原博文依然用到了 lambda 表达式
>>> # 我就再生搬硬套一次, 把上面的复制过来
>>> # 蹩脚地用一个全局变量i, 和映射函数mapper()

>>> i = 0
>>> def mapper(x): # 映射函数即 mapper
    global i
    x += '_' + str(i)
    i += 1
    return x
dataset.rename(columns=mapper, inplace=True)

To summarize a little: the use of dictionary derivation and list derivation are very similar, the largest The difference is whether to choose square brackets or curly brackets

The above is the detailed content of Introduction to the method of modifying DataFrame column names in pandas (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete