小编Zaf*_*rmk的帖子

如何在 pandas .groupby 之后访问列

我有一个数据框,我使用了 .groupby() 和 .agg() 函数。

movieProperties = combined_df.groupby(['movieId', 'title', 'genres']).agg({'rating': ['count', 'mean']})

这是创建新数据框的代码。但是我似乎无法再以相同的方式访问列了。如果我尝试,movieProperties['genres']我总是会收到 KeyError。如何再次访问这个新数据框中的列?

python pandas pandas-groupby

9
推荐指数
1
解决办法
2万
查看次数

将列表映射到1和0

我有两个列表my_genre和list_of_genres。我想要一个函数来检查是否my_list[index]list_of_genres并转换list_of_genres[index2]1if的情况。

list_of_genres = ['Adventure', 'Animation', 'Children', 'Comedy', 'Fantasy', 'Drama', 'Romance', 'Action', 'Thriller', 'Sci-Fi', 'Crime', 'Horror', 'Mystery', 'IMAX', 'Documentary', 'War', 'Musical', 'Western', 'Film-Noir']


my_genre = ['Action', 'Crime', 'Drama', 'Thriller']
Run Code Online (Sandbox Code Playgroud)

预期结果:

[0 0 0 0 0 1 0 1 1 0 1 0 0 0 0 0 0 0 0]
data type : np.array
Run Code Online (Sandbox Code Playgroud)

最终,我想将执行此操作的功能应用于包含流派的pandas列。

python numpy list pandas data-science

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

标签 统计

pandas ×2

python ×2

data-science ×1

list ×1

numpy ×1

pandas-groupby ×1