首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何转换np.where(),同时把熊猫转换成考拉?

如何转换np.where(),同时把熊猫转换成考拉?
EN

Stack Overflow用户
提问于 2021-12-28 17:39:00
回答 2查看 261关注 0票数 -1

为了扩展性,我把一些熊猫系列和熊猫数据转换成考拉。但是在我使用np.where()的地方,我试着通过考拉,就像它以前通过熊猫的数据一样。但我得到了一个错误,一个PandasNotImplementedError。

我如何克服这个错误?我试过ks.where(),但没成功。

这是我正在使用熊猫的代码的模型。

代码语言:javascript
运行
复制
import pandas as pd
import numpy as np
pdf = np.where(condition, action1, action2)

如果我使用toPandas()from_pandas()将考拉转换回熊猫,那么代码就能工作,但是由于性能和可伸缩性的原因,我不能使用熊猫。如果可能的话,请建议我在考拉中使用另一种方法,或者为numpy提供一个可供选择的库,它可以很好地处理考拉。

EN

回答 2

Stack Overflow用户

发布于 2022-02-23 15:10:30

根据考拉的文档 (1.8.2),databricks.koalas.DataFramedatabricks.koalas.Series上的where函数在条件为False时只接受两个参数:条件和值。无论条件是True,值都不会更改。它的行为类似于它在熊猫中的表现。

因此,可以像这样使用where语句的链接:

代码语言:javascript
运行
复制
kdf.where(condition, action2).where(~condition, action1)
# action1 --> Action when condition is True.
# action2 --> Action when condition is False.

# The output of this cannot be assigned back to a column though. To assign the output to some column, the where has to be applied on a Series.
kdf['some_column'].where(condition, action2).where(~condition, action1)

另外,请注意,在考拉上,可以将databricks.koalas.Series上的where条件分配回列,但不能将where条件的输出应用到databricks.koalas.DataFrame上,就像在Pandas中那样。

票数 1
EN

Stack Overflow用户

发布于 2021-12-28 17:48:29

我对考拉并不太熟悉,但我认为使用DataFrame.where()的东西会有效果。

例如:

代码语言:javascript
运行
复制
from databricks.koalas.config import set_option, reset_option
set_option("compute.ops_on_diff_frames", True)
df1 = ks.DataFrame({'A': [0, 1, 2, 3, 4], 'B':[100, 200, 300, 400, 500]})
df2 = ks.DataFrame({'A': [0, -1, -2, -3, -4], 'B':[-100, -200, -300, -400, -500]})
df1.where(df1 > 1, df2)

如果需要的话,还有相应的考拉Series.where()。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70510580

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档