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 )