如何在Dask Dataframe上输入列值?

时间:2018-03-25 15:16:09

标签: pandas dataframe dask

我想暗示Dask Dataframe的负值,使用此代码的pandas:

1st subscriber: one
2nd subscriber: one
1st subscriber: two
2nd subscriber: two

1 个答案:

答案 0 :(得分:3)

我认为需要dask.dataframe.Series.clip_lower

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

<强>示例

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

对于更一般的解决方案,请使用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