为了扩展性,我把一些熊猫系列和熊猫数据转换成考拉。但是在我使用np.where()
的地方,我试着通过考拉,就像它以前通过熊猫的数据一样。但我得到了一个错误,一个PandasNotImplementedError。
我如何克服这个错误?我试过ks.where()
,但没成功。
这是我正在使用熊猫的代码的模型。
import pandas as pd
import numpy as np
pdf = np.where(condition, action1, action2)
如果我使用toPandas()
或from_pandas()
将考拉转换回熊猫,那么代码就能工作,但是由于性能和可伸缩性的原因,我不能使用熊猫。如果可能的话,请建议我在考拉中使用另一种方法,或者为numpy提供一个可供选择的库,它可以很好地处理考拉。
发布于 2022-02-23 15:10:30
根据考拉的文档 (1.8.2),databricks.koalas.DataFrame
和databricks.koalas.Series
上的where函数在条件为False
时只接受两个参数:条件和值。无论条件是True
,值都不会更改。它的行为类似于它在熊猫中的表现。
因此,可以像这样使用where语句的链接:
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中那样。
发布于 2021-12-28 17:48:29
我对考拉并不太熟悉,但我认为使用DataFrame.where()的东西会有效果。
例如:
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()。
https://stackoverflow.com/questions/70510580
复制相似问题