DataFrame 按照字符串长度进行排序 iat 和 iloc两种不同方式的耗时

数据共有如下几列,100行左右

Index(['区域', '网点名称', '网点编码', 'S', 'M', 'L', 'XL', 'XXL', 'XXXL', '4XL', '5XL', '总计:']

''' 法一 使用循环 按照行遍历'''
time1_1 = datetime.datetime.now()
clothes['LEN'] = numpy.nan
for row_loc in range(len(clothes)):
    clothes.iloc[row_loc,12] = len(clothes.iloc[row_loc,1]) 
clothes.sort_values(by='LEN', inplace=True)
time1_2 = datetime.datetime.now()
time = {'time1':time1_2 - time1_1}

'''法二 使用apply'''
def assign_to_len(row):
    row['LEN1'] = len(row['网点名称'])
    return row
time2_1 = datetime.datetime.now()
clothes = clothes.apply(assign_to_len,axis=1)
clothes.sort_values(by='LEN', inplace=True)
time2_2 = datetime.datetime.now()

法一法二耗时比较:

In [4]: time

Out[4]:

{'time1': datetime.timedelta(0, 0, 45003),

'time2': datetime.timedelta(0, 0, 75004)}


当法一将iloc换为iat后耗时比较:

In [7]: time

Out[7]: 

{'time1': datetime.timedelta(0, 0, 4000),

 'time2': datetime.timedelta(0, 0, 75005)}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值