from pandas import DataFrame,Series import pandas as pd import numpy as np data = DataFrame({'k1':['A']*3+['B']*4, 'k2':[1,1,2,3,3,4,4]}) print(data) ''' k1 k2 0 A 1 1 A 1 2 A 2 3 B 3 4 B 3 5 B 4 6 B 4 ''' # 编写一个映射 a_to_b = { 'A':'ONE', 'B':'TWO', 'C':'THREE' } data['k3'] = data['k1'].map(a_to_b) print(data) ''' k1 k2 k3 0 A 1 ONE 1 A 1 ONE 2 A 2 ONE 3 B 3 TWO 4 B 3 TWO 5 B 4 TWO 6 B 4 TWO ''' data['k4'] = data['k1'].map(lambda x:a_to_b[x]) print(data) ''' k1 k2 k3 k4 0 A 1 ONE ONE 1 A 1 ONE ONE 2 A 2 ONE ONE 3 B 3 TWO TWO 4 B 3 TWO TWO 5 B 4 TWO TWO 6 B 4 TWO TWO '''