Pun*_*thi 8 python pandas dask
我需要创建一个基于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'] ]
Run Code Online (Sandbox Code Playgroud)
在dask中,我必须做同样的事情,如下所示:
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()
Run Code Online (Sandbox Code Playgroud)
问题:
答案:
你现在所做的几乎没问题。在您compute准备好接受最终答复之前,您无需致电。
# ddf1 = ddf.assign(col1 = list(ddf.shop_week.apply(f).compute()))
ddf1 = ddf.assign(col1 = ddf.shop_week.apply(f))
Run Code Online (Sandbox Code Playgroud)
对于某些情况dd.Series.where可能很合适
ddf1 = ddf.assign(col1 = ddf.shop_week.where(cond=ddf.balance > 0, other=0))
Run Code Online (Sandbox Code Playgroud)从 0.10.2 版本开始,您现在可以将列直接插入 dask.dataframes
ddf['col'] = ddf.shop_week.apply(f)
Run Code Online (Sandbox Code Playgroud)| 归档时间: |
|
| 查看次数: |
1042 次 |
| 最近记录: |