Numpy 与 Pandas 库小代码

 

 1、Pandas .apply() 函数内部处理数据进度条/计数器 怎么实现

         思想:利用 global 、local、nonlocal 等关键字

                   参考:python之旅基础——全局变量和局部变量

# 进度条
def process_bar(percent, start_str='start', end_str='100%', total_length=0):
    bar = '\r' + start_str+ '       {:0>4.1f}%|'.format(percent * 100) + end_str
    print(bar, end='', flush=True)


bar_num = 1
def apply_fun(url,raws_num):
    # 定义改变上一层变量关键字
    nonlocal bar_num
    process_bar(percent = bar_num*1.0 / raws_num)
    bar_num += 1
    print(bar_num)

null_df['vector'] = null_df['vector'].apply(apply_fun,args=(raws_num,))

 

 

 

 

你可能感兴趣的:(AI_小代码库,python)