12

dask データフレームの条件に基づいた列を作成する必要があります。パンダでは、それはかなり簡単です:

ddf['TEST_VAR'] = ['THIS' if x == 200607 else  
              'NOT THIS' if x == 200608 else 
              'THAT' if x == 200609 else 'NONE'  
              for x in ddf['shop_week'] ]

While in dask I have to do same thing like below:

def f(x):
    if x == 200607:
         y= 'THIS'
    elif x == 200608 :
         y= 'THAT'
    else :
         y= 1 
    return y

ddf1 = ddf.assign(col1 = list(ddf.shop_week.apply(f).compute()))
ddf1.compute()

Questions:

  1. Is there a better/more straightforward way to achieve it?
  2. I can't modify the first dataframe ddf, i need to create ddf1 to se the changes is dask dataframe Immutable object?
4

3 に答える 3

7

答え:

  1. あなたが今やっていることは、ほとんど大丈夫です。compute最終的な回答の準備が整うまで、電話する必要はありません。

    # ddf1 = ddf.assign(col1 = list(ddf.shop_week.apply(f).compute()))
    ddf1 = ddf.assign(col1 = ddf.shop_week.apply(f))
    

    場合によっては、適切な場合dd.Series.whereがあります

    ddf1 = ddf.assign(col1 = ddf.shop_week.where(cond=ddf.balance > 0, other=0))
    
  2. バージョン 0.10.2 以降、列を直接 dask.dataframes に挿入できるようになりました

    ddf['col'] = ddf.shop_week.apply(f)
    
于 2016-07-27T12:48:02.773 に答える