pandas set_value用法

原始代码:
df_valid_inventory_sel_tmp['col1']=-9999
for index in df_valid_inventory_sel_tmp.index:
    time_stamp = time.time()
    df_valid_inventory_sel_tmp['col1'][index] =su.sta_mean(index)  ##传统拷贝 40ms
    #df_valid_inventory_sel_tmp.set_value(index,'mean_lasteN',su.sta_mean(index)) ##优化赋值,10ms以内
    print(str(time.time() - time_stamp))
报“Warning”: A value is trying to be set on a copy of a slice from a DataFrame
 
  

  优化后代码:

#df_valid_inventory_sel_tmp['col1']=-9999
for index in df_valid_inventory_sel_tmp.index:
    time_stamp = time.time()
   #df_valid_inventory_sel_tmp['col1'][index] =su.sta_mean(index)  ##传统拷贝 40ms
    df_valid_inventory_sel_tmp.set_value(index,'mean_lasteN',su.sta_mean(index)) ##优化赋值,10ms以内
    print(str(time.time() - time_stamp))
用set_value比直接赋值快4倍左右

你可能感兴趣的:(python)