我对考拉很陌生。我被告知在我的工作中要执行考拉,而不是熊猫。早些时候,当我们有数据的时候,我们把它转换成熊猫,并使用它作为np.where,并在里面进行状态检查。例如,在熊猫上,我们过去经常这样做
np.where(condition,action1,action2)当我试着用考拉做同样的事情时,我们会发现下面的错误
PandasNotImplementedError:方法
pd.Series.__iter__()没有实现。如果要将数据收集为NumPy数组,请使用“to_numpy()”。
我甚至尝试过ks.series和ks.dataframe,但是错误没有发生。
考拉是否有任何方法或功能来接受3个参数(条件,action1,action2),就像我们在熊猫上使用np.where一样。如果任何人也通过例子来解释的话,这将是非常有帮助的。
发布于 2021-12-30 11:19:27
与np.where类似的一个可能的解决方案是自己创建一个使用考拉where的函数(如下所示)
import databricks.koalas as ks
# sample dataframe
df = ks.DataFrame({
'id': [1, 2, 3, 4, 5],
'cost': [5000, 4000, 3000, 4500, 2000],
'class': ['A', 'A', 'B', 'C', 'A']
})
# your custom function
def numpy_where(s, cond, action1, action2):
return s.where(cond, action2).where(~cond, action1)
# create sample new column
df['new_col'] = numpy_where(df['class'], df['class'] == 'A', 'yes', 'no')
print(df)
# id cost class new_col
# 0 1 5000 A yes
# 1 2 4000 A yes
# 2 3 3000 B no
# 3 4 4500 C no
# 4 5 2000 A yes基本上:
s是计算wherecond的条件,satisfiedaction1是要插入的值,cond是trueaction2,是cond是false时要插入的值
https://stackoverflow.com/questions/70510056
复制相似问题