按行遍歷
測試程序:對一個10萬行的datafeame數(shù)據(jù)先分組 再按行遍歷 計(jì)算總個數(shù)
最快的方式:轉(zhuǎn)換成數(shù)組,再使用for循環(huán)
count = 0
start = time()
for userid,user_group in test_data[0:100000].groupby(by=['userid']):
for index,row in enumerate(user_group.values):
count +=1
stop = time()
print str(count)
print(str(stop-start) + '秒卜范!')
print 'ok!'
輸出:12.504999876秒海雪!
使用內(nèi)置的iterrows函數(shù):
count = 0
start = time()
for userid,user_group in test_data[0:100000].groupby(by=['userid']):
for index,row in user_group.iterrows():
count +=1
stop = time()
print str(count)
print(str(stop-start) + '秒奥裸!')
print 'ok!'
輸出:20.3080000877秒沪袭!
使用內(nèi)置的itertuples函數(shù) 最慢:
count = 0
start = time()
for userid,user_group in test_data[0:100000].groupby(by=['userid']):
for row in user_group.itertuples():
count +=1
stop = time()
print str(count)
print(str(stop-start) + '秒!')
print 'ok!'
輸出:110.143000126秒!