簡體   English   中英

將值分配給 python 中的行的最快方法是什么?

[英]What's the fastest way to assign values to rows in python?

我有一個運行模擬 21 天的代碼,其中 n 個訂單每天到達,並且必須將一些值分配給從隨機抽樣另一個數據幀生成的訂單。 這個模擬必須運行 100 次才能計算一些累積統計數據。 我嘗試了兩種方法,但它們需要很長時間,所以我正在尋找一種方法來加快速度。

方法#1:

def get_values():
    index=random.randint(0,(len(shipments)-1))#generate number
    country,segment,time,weight,costs,carrier=shipments[['SHIP TO COUNTRY','SEGMENT','TRANSIT TIME','OTM_WEIGHT','TOTAL_TRANSPORT_COST','CARRIER']].iloc[index] #data sampling using the random number created
    costs=round(costs,2)
    return[country,segment,time,weight,costs,carrier]
def get_arrival(): 
    index=random.randint(0,len(daily_arrivals)-1)
    arrival=(daily_arrivals['DELIVERY DATE'].iloc[index])
    return(arrival)
def LC_run(env,df,j,balance,dispatch,orders):
    while True:
      arrival=get_arrival()
      for m in range(arrival):
          order_nmbr='order #{}'.format(m)
          pickup=env.now
          country,segment,time,weight,cost,carrier=get_values()
          delivery=(pickup+time)
          df.loc[len(df)]=[order_nmbr,country,segment,pickup,delivery,time,weight,cost,carrier,j]
          balance+=cost
          dispatch+=weight
          orders+=1
      
      yield env.timeout(1)
      

df=pd.DataFrame(columns=['Order #','SHIP TO COUNTRY','SEGMENT','PICK UP DATE','DELIVERY DATE','TRANSIT TIME','OTM_WEIGHT', 'TRANSPORTATION COST','CARRIER','SIM_RUN']) 
pdf=pd.DataFrame(columns=['SIM_RUN','TOTAL_TRANSPORTATION_COST','TOTAL_WEIGHT','TOTAL_NUMBER_OF_ORDERS'])  
for j in range(10):
    global balance, dispatch, orders
    balance=0
    dispatch=0
    orders=0
   #how many times is the simulation running 
    env = simpy.Environment()
    env.process(LC_run(env,df,j,balance,dispatch,orders))    
    env.run(until=21) #how long is the simulation running until
    
    pdf.loc[len(pdf)]=[j,balance,dispatch,orders]
    print('run #{}'.format(j))

方法#2 *注意接送時間並不重要,但在這一點中還需要額外添加我運行模擬一天而不是 21 天,而不是有 n 個到達我有到達率

def get_values():
    index=random.randint(0,(len(shipments)-1))#generate number
    country,segment,time,weight,costs,carrier=shipments[['SHIP TO COUNTRY','SEGMENT','TRANSIT TIME','OTM_WEIGHT','TOTAL_TRANSPORT_COST','CARRIER']].iloc[index] #data sampling using the random number created
    costs=round(costs,2)
    return[country,segment,time,weight,costs,carrier]
def get_arrival(): 
    index=random.randint(0,len(daily_arrivals)-1)
    arrival=(24/daily_arrivals['DELIVERY DATE'].iloc[index])
    #print(arrival)
    return(arrival)

def LC_run(env,df,i,arival,j,balance,dispatch,orders):
    while True:

      order_nmbr='order #{}'.format(orders)
      pickup=i
      pick_up_time=('{} day, {} hours').format(i, i+(env.now*24))
      country,segment,time,weight,cost,carrier=get_values()
      delivery=(pickup+time)
      df.loc[len(df)]=[order_nmbr,country,segment,pickup,pick_up_time,delivery,time,weight,cost,carrier,j]
      
      yield env.timeout(arrival)
      balance+=cost
      dispatch+=weight
      orders+=1

df=pd.DataFrame(columns=['Order #','SHIP TO COUNTRY','SEGMENT','PICK UP DATE','PICKUP TIME','DELIVERY DATE','TRANSIT TIME','OTM_WEIGHT', 'TRANSPORTATION COST','CARRIER','SIM_RUN']) 
pdf=pd.DataFrame(columns=['SIM_RUN','TOTAL_TRANSPORTATION_COST','TOTAL_WEIGHT','TOTAL_NUMBER_OF_ORDERS'])  
for j in range(10):
    global balance, dispatch, orders
    balance=0
    dispatch=0
    orders=0
    for i in range(21):#how many times is the simulation running 
        env = simpy.Environment()
        arrival=get_arrival()
        env.process(LC_run(env,df,i,arrival,j,balance,dispatch,orders))    
        env.run(until=24) #how long is the simulation running until
    
    pdf.loc[len(pdf)]=[j,balance,dispatch,orders]
    print('run #{}'.format(j))

loc 不是那么快, df.iloc[-1]可以加快,這里有 3 種不同的方法:

%timeit df.loc[len(df)] = [99, 'abc', .23, now, 99]
2.61 ms ± 94.2 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

%timeit df.values[-1,:] = [99, 'abc', .23, now, 99]
5.59 ms ± 291 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

%timeit df.iloc[-1] = [99, 'abc', .23, now, 99]
1.03 ms ± 88.3 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM