按行遍历
测试程序:对一个10万行的datafeame数据先分组 再按行遍历 计算总个数
最快的方式:转换成数组,再使用for循环
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秒!
使用内置的iterrows函数:
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秒!
使用内置的itertuples函数 最慢:
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秒!