相关疑难解决方法(0)

在极坐标数据框中每行应用 Python UDF 函数会引发意外异常“预期元组,得到列表”

我在 Python 中有以下极坐标 DF

df = pl.DataFrame({
    "user_movies": [[7064, 7153, 78009], [6, 7, 1042], [99, 110, 3927], [2, 11, 152081], [260, 318, 195627]],
    "user_ratings": [[5.0, 5.0, 5.0], [4.0, 2.0, 4.0], [4.0, 4.0, 3.0], [3.5, 3.0, 4.0], [1.0, 4.5, 0.5]],
    "common_movies": [[7064, 7153], [7], [110, 3927], [2], [260, 195627]]
})
print(df.head())
Run Code Online (Sandbox Code Playgroud)

我想创建一个名为“common_movie_ ratings”的新列,该列将从每个评级列表中仅获取常见电影中评级的电影的索引。例如,对于第一行,我应该仅返回电影的评分 [7064, 7153,],对于第二行,我应该返回电影的评分 [7],依此类推。

为此,我创建了以下函数:

def get_common_movie_ratings(row): #Each row is a tuple of arrays.
    common_movies = row[2] #the index of the tuple denotes the 3rd array, which represents the …
Run Code Online (Sandbox Code Playgroud)

python user-defined-functions dataframe python-polars

4
推荐指数
1
解决办法
446
查看次数