我有两个数据帧-一个是原始数据,另一个是原始数据的映射/分类器。我想遍历原始数据并根据另一个返回分类。在
df=
Artist Genres Image Popularity Followers Americana Around the World BritRock ... Pops Post-Punk / Angular Progressive Psych' Punky Shoegazer / Dreamer Soul / Funk Soundtracks Younger Rap Younget Indie
0 0 Buke and Gase [brooklyn indie, deep indie rock] https://i.scdn.co/image/eece57650f99d1265f871a... 32 9328 ...
1 0 Bright Light Bright Light [austindie] https://i.scdn.co/image/5234fdee902fe1d4d5ad20... 39 23153 ...
2 0 Angelo De Augustine [preverb, small room] https://i.scdn.co/image/3080e9d856e639d539804b... 45 6393 ...
3 0 Modeselektor [alternative dance, electronic, indietronica, ... https://i.scdn.co/image/1bf7a85bcc0c047d8914a2... 50 120084 ...
4 0 Razorlight [britpop, garage rock, indie rock, modern rock... https://i.scdn.co/image/b743a5f809f671be6a60f7... 63 252969 ...
5 rows × 33 columns
分类器:
^{pr2}$我想在df['Genres']上迭代,如果任何字符串与分类器['spotify_genre']匹配,那么它应该将计数1返回到df中由分类器['class_one']决定的必要列 例如Buke和Gase有一个类型“BrooklynIndie”,它应该为原始df中的“Younger indie”列返回“1”。在
我试过很多不同的方法来解决这个问题,但找不出最好的办法。在
我会分两步来完成。首先使用dict将值映射到原始数据框中的一列中:
其中dict是一个格式为dict的字典
^{pr2}$然后您可以在
df['class']
上使用pandas.get_dummies()
来获得所需的所有列。在相关问题 更多 >
编程相关推荐