Dask equivalent to Pandas replace?

≯℡__Kan透↙ 提交于 2019-11-29 22:34:55

问题


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)

回答1:


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')



回答2:


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



来源:https://stackoverflow.com/questions/40899044/dask-equivalent-to-pandas-replace

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!