5

I would like to impute negative values of Dask Dataframe, with pandas i use this code:

df.loc[(df.column_name < 0),'column_name'] = 0
ambigus9
  • 1,417
  • 3
  • 19
  • 37

1 Answers1

4

I think need dask.dataframe.Series.clip_lower:

ddf['B'] = ddf['B'].clip_lower(0)

Sample:

import pandas as pd

df = pd.DataFrame({'F':list('abcdef'),
                   'B':[-4,5,4,-5,5,4],
                   'A':list('aaabbb')})

print (df)
   A  B  F
0  a -4  a
1  a  5  b
2  a  4  c
3  b -5  d
4  b  5  e
5  b  4  f

from dask import dataframe as dd 
ddf = dd.from_pandas(df, npartitions=3)
#print (ddf)

ddf['B'] = ddf['B'].clip_lower(0)
print (ddf.compute())
   A  B  F
0  a  0  a
1  a  5  b
2  a  4  c
3  b  0  d
4  b  5  e
5  b  4  f

For more general solution use dask.dataframe.Series.mask`:

ddf['B'] = ddf['B'].mask(ddf['B'] > 0, 3)
print (ddf.compute())
   A  B  F
0  a -4  a
1  a  3  b
2  a  3  c
3  b -5  d
4  b  3  e
5  b  3  f
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252