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)
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')
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With