Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dask equivalent to Pandas replace?

Tags:

pandas

dask

Something I use regularly in pandas is the .replace operation. I am struggling to see how one readily performs this same operation on a dask dataframe?

df.replace('PASS', '0', inplace=True)
df.replace('FAIL', '1', inplace=True)
like image 227
docross Avatar asked Nov 30 '16 22:11

docross


2 Answers

You can use mask:

df = df.mask(df == 'PASS', '0')
df = df.mask(df == 'FAIL', '1')

Or equivalently chaining the mask calls:

df = df.mask(df == 'PASS', '0').mask(df == 'FAIL', '1')
like image 127
root Avatar answered Dec 04 '22 19:12

root


If anyone would like to know how to replace certain values in a specific column, here's how to do this:

def replace(x: pd.DataFrame) -> pd.DataFrame:
    return x.replace(
      {'a_feature': ['PASS', 'FAIL']},
      {'a_feature': ['0', '1']}
    )
df = df.map_partitions(replace)

Since we operate on a pandas' DataFrame here, please refer to the documentation for further information

like image 23
LiquidSpirit Avatar answered Dec 04 '22 18:12

LiquidSpirit