程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[pandas tip] change field field name

編輯:Python
Enjoy the beautiful picture 2022/06/22

In the normal development requirements , Operations involving changing field column names , We can use .rename Method Implement the requirement

The following provides two practice cases to repeat and learn , For future reference  

Practice cases 1 

import pandas as pd
df = pd.DataFrame([['L123','A',0,123],
['L456','A',1,456],
['L437','C',0,789],
['L112','B',1,741],
['L211','A',0,852],
['L985','B',1,963]
],columns=['Raw Material','Level','Passing','l/t'])
# change 'Raw Material' and 'l/t' Name of two fields
df = df.rename(columns = {'Raw Material':'Material','l/t':'LT'})

df

df( After processing )

Practice cases 2 

import pandas as pd
cal_supply = pd.DataFrame([['L123',1,2,3],
['L456',4,5,6],
['L437',7,8,9],
['L112',10,11,12],
['L211',13,14,15],
['L985',16,17,18]
],columns=['Material','W1|6/22','W2|6/23','W3|6/24'])
# Replace cal_supply In the table W1-W3 Column name of
new_dict = {
key:key.split('|')[1]
for i, key in enumerate(cal_supply.columns.tolist()[1:])
}
cal_supply.rename(columns=new_dict, inplace=True)

cal_supply

new_dict 

cal_supply( After processing ) 


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved