1.替换
1.1 以{字典}的形式替换
df = pd.DataFrame( {'AAA' : [1,2,1,3], 'BBB' : [1,1,2,2], 'CCC' : [2,1,3,1]}); df
source_cols = df.columns
new_cols = [str(x) + "_cat" for x in source_cols]
S = {1 : 'Alpha', 2 : 'Beta', 3 : 'Charlie' }
df[new_cols] = df[source_cols].applymap(S.get);df
source_cols = df.columns[0:3]
new_cols_1 = [str(x) + "_cat_" for x in source_cols]
S = {1 : 'Alpha', 2 : 'Beta', 3 : 'Charlie' }
df[new_cols_1]=df[source_cols].replace(S)
1.2 以 [列表] 形式的替换
new_cols_1 = [str(x) + "_cat_1" for x in source_cols]
S = ['Alpha', 'Beta','Charlie']
L=[1,2,3]
df[new_cols_1]=df[source_cols].replace(L,S)