Dataframe 为Dask中的列指定条件值

Dataframe 为Dask中的列指定条件值,dataframe,dask,Dataframe,Dask,我试图对特定列的行进行条件赋值:target。我做了一些研究,似乎这里给出了答案: 我将再现我的需要。模拟数据集: x = [3, 0, 3, 4, 0, 0, 0, 2, 0, 0, 0, 6, 9] y = [200, 300, 400, 215, 219, 360, 280, 396, 145, 276, 190, 554, 355] mock = pd.DataFrame(dict(target = x, speed = y)) mock的外观是: 有了此数据帧,我将其转换为Dask数

我试图对特定列的行进行条件赋值:target。我做了一些研究,似乎这里给出了答案:

我将再现我的需要。模拟数据集:

x = [3, 0, 3, 4, 0, 0, 0, 2, 0, 0, 0, 6, 9]
y = [200, 300, 400, 215, 219, 360, 280, 396, 145, 276, 190, 554, 355]
mock = pd.DataFrame(dict(target = x, speed = y))
mock的外观是:

有了此数据帧,我将其转换为Dask数据帧:

我应用我的条件规则:目标中高于0的所有值都必须为1,其他所有值都必须为0二进制化目标。按照上述线程,它应该是:

result = mock_dask.target.where(mock_dask.target > 0, 1)
我查看了结果数据集,但它没有按预期工作:

In [7]: result.head(7)
Out [7]:
0    3
1    1
2    3
3    4
4    1
5    1
6    1
Name: target, dtype: object 
正如我们所看到的,mock和result中的列目标不是预期的结果。我的代码似乎正在将所有0个原始值转换为1,而不是将大于0的值转换为条件规则中的1


Dask新手,提前谢谢你的帮助

对我来说,它们似乎是一样的

In [1]: import pandas as pd

In [2]: x = [1, 0, 1, 1, 0, 0, 0, 2, 0, 0, 0, 6, 9]
   ...: y = [200, 300, 400, 215, 219, 360, 280, 396, 145, 276, 190, 554, 355]
   ...: mock = pd.DataFrame(dict(target = x, speed = y))
   ...: 

In [3]: import dask.dataframe as dd

In [4]: mock_dask = dd.from_pandas(mock, npartitions = 2)

In [5]: mock.target.where(mock.target > 0, 1).head(5)
Out[5]: 
0    1
1    1
2    1
3    1
4    1
Name: target, dtype: int64

In [6]: mock_dask.target.where(mock_dask.target > 0, 1).head(5)
Out[6]: 
0    1
1    1
2    1
3    1
4    1
Name: target, dtype: int64
好的,中的文档非常清楚。多亏@MRocklin的反馈,我意识到了我的错误。在文档中,函数列表中的最后一个函数使用以下语法:

DataFrame.where(cond[, other])      Return an object of same shape as self and whose corresponding entries are from self where cond is True and otherwise are from other.
因此,正确的代码行是:

result = mock_dask.target.where(mock_dask.target <= 0, 1) 

这是预期的输出。

Hi@MRocklin感谢您的回复。为了更好地理解,我编辑了我的问题,似乎我的代码行正在将所有0转换为1。而不是1或大于1:1的值,这是所需的输出。
DataFrame.where(cond[, other])      Return an object of same shape as self and whose corresponding entries are from self where cond is True and otherwise are from other.
result = mock_dask.target.where(mock_dask.target <= 0, 1) 
In [7]: result.head(7)
Out [7]:
0    1
1    0
2    1
3    1
4    0
5    0
6    0
Name: target, dtype: int64