如何在将熊猫转换为考拉的同时转换np.where()



为了可扩展性,我将一些pandas系列和pandas数据帧转换为考拉。但在我使用np.where()的地方,我试图传递考拉数据帧,就像以前传递熊猫数据帧一样。但是我得到了一个错误PandasNotImplementedError。

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

这是我正在使用panda编写的代码模型。

import pandas as pd
import numpy as np
pdf = np.where(condition, action1, action2)

如果我使用toPandas()from_pandas()将考拉转换回panda,代码就可以工作了,但由于性能和可扩展性的原因,我不能使用panda。如果可能的话,请给我一个考拉的替代方法,或者一个numpy的替代库,它可以很好地处理考拉。

根据Koalas(1.8.2(的文档,当条件为False时,databricks.koalas.DataFramedatabricks.koalas.Series上的where函数只接受两个参数,条件和值。只要条件为True,则该值不会更改。它的行为与熊猫的行为相似。

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

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中一样。

我对考拉不太熟悉,但我认为使用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)

还有一个相应的考拉系列。where((,如果你需要的话。

最新更新