Dask Updating列类似于sql case



我有一个带有列";is_internal;类型为int64。我想更新类似于SQL案例语句的内容:

CASE WHEN ltrim(rtrim(is_internal)) = '1' then 'Internal' else 'External' END as type

将数据导入为:

import pandas as pd 
import dask.dataframe as dd
import time
t=time.process_time()
df_train = dd.read_csv(r"C:test.bcp", sep='t', sample=25000000)

通常在熊猫身上,我会做一些类似的事情,但这占用了很多空间,我对此很有限

df_train.loc[df_train['is_internal'] == 1, 'type'] = 'internal'
df_train.loc[df_train['is_internal'] == 0, 'type'] = 'external'

使用dask不会占用大量空间/内存的最佳方法是什么?

您不应该更改Dask对象。使用.where方法可以实现您想要的。不幸的是,许多人发现它的表述令人困惑;但是在许多情况下,如果您想直接使用pandas方法进行处理,则可以使用map_partition:包装您的代码

def simple_where(df):
df.loc[df['is_internal'] == 1, 'type'] = 'internal'
df.loc[df['is_internal'] == 0, 'type'] = 'external'
return df
df_out = df_train.map_partitions(simple_where)

最新更新