用字典有效地替换熊猫系列中的值
s
d
s.replace(d)
s.map(d)
s.replace
s.map(d)
s.replace
import pandas as pd, numpy as np df = pd.DataFrame({'A': np.random.randint(0, 1000, 1000000)})lst = df['A'].values.tolist()##### TEST 1 #####d = {i: i+1 for i in range(1000)}%timeit df['A'].replace(d) # 1.98s%timeit [d[i] for i in lst] # 134ms##### TEST 2 #####d = {i: i+1 for i in range(10)}%timeit df['A'].replace(d) # 20.1ms%timeit [d.get(i, i) for i in lst] # 243ms
注:
智慧大石
相关分类