pandas df 遍历方法晓述

pandas 遍历有以下三种访法。
性能如下
.iterrows():在单独的变量中返回索引和行项目,但显着较慢
.itertuples():快于.iterrows(),但将索引与行项目一起返回,ir [0]是索引
zip:最快,但不能访问该行的索引

用法如下:

t = pd.DataFrame({'a': range(0, 10000), 'b': range(10000, 20000)})
B = []
C = []
A = time.time()
for i,r in t.iterrows():
    C.append((r['a'], r['b']))
B.append(time.time()-A)

C = []
A = time.time()
for ir in t.itertuples():
    C.append((ir[1], ir[2]))    
B.append(time.time()-A)

C = []
A = time.time()
for r in zip(t['a'], t['b']):
    C.append((r[0], r[1]))
B.append(time.time()-A)

print B

你可能感兴趣的:(pandas)