将熊猫数据框列拆分为多个并遍历它

我正在尝试采用具有匹配 ID 的艺术家,使音乐跨越各种单一到流派的组合。


这就是我想要做的


Artist | Id | Genre                | Jazz | Blues | Rock | Trap | Rap | Hip-Hop | Pop | Rb  |

----------------------------------------------------------------------------------------------------

Bob    | 1  | [Jazz, Blues]        |   1  |   1   |   0  |   0  |   0 |   0     |  0  |   0

----------------------------------------------------------------------------------------------------

Fred   | 2  | [Rock,Jazz]          |   1  |   0   |   1  |   0  |   0 |   0     | 0   |   0

----------------------------------------------------------------------------------------------------

Jeff   | 3  | [Trap, Rap, Hip-Hop] |   0  |   0   |   0  |   1  |   1 |   1     | 0   |   0

----------------------------------------------------------------------------------------------------

Amy    | 4  | [Pop, Rock, Jazz]    |   1  |   0   |   1  |   0  |   0 |   0     | 1   |   0

----------------------------------------------------------------------------------------------------

Mary   | 5  | [Hip-Hop, Jazz, Rb]  |   1  |   0   |   0  |   0  |   0 |   1     | 0   |   1

----------------------------------------------------------------------------------------------------

这是我得到的错误


---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-50-7a4ed81e14d7> in <module>

     11 for index, row in artist_df.iterrows():

     12     x.append(index)

---> 13     for i in row['genre']:

     14         artists_with_genres.at[index, genre] = 1

     15 


TypeError: 'float' object is not iterable

这些(艺术家)流派是我将在结合其他因素(如年份、歌曲或人口统计数据)时用来帮助确定相似艺术家的属性。


我正在创建和迭代的新专栏将指定艺术家是否属于某个流派。用 1/0 来简单地表示艺术家是否是摇滚/嘻哈/陷阱等。使用属性的二进制表示。


这是当前的数据框

http://img.mukewang.com/63aa9d430001e17305710498.jpg

获取我的数据框并将流派拆分为单独的类型,以便我可以转换为 1/0 二进制表示。

我需要将流派设置为索引吗?


萧十郎
浏览 91回答 1
1回答

aluckdog

尝试使用get_dummies:df['Genre'] = df['Genre'].str.split('|')dfx = pd.get_dummies(pd.DataFrame(df['Genre'].tolist()).stack()).sum(level=0)df = pd.concat([df, dfx], axis=1).drop(columns=['Genre'])print(df)&nbsp; Artist&nbsp; Id&nbsp; Blues&nbsp; Hip-Hop&nbsp; Jazz&nbsp; Pop&nbsp; Rap&nbsp; Rb&nbsp; Rock&nbsp; Trap0&nbsp; &nbsp; Bob&nbsp; &nbsp;1&nbsp; &nbsp; &nbsp; 1&nbsp; &nbsp; &nbsp; &nbsp; 0&nbsp; &nbsp; &nbsp;1&nbsp; &nbsp; 0&nbsp; &nbsp; 0&nbsp; &nbsp;0&nbsp; &nbsp; &nbsp;0&nbsp; &nbsp; &nbsp;01&nbsp; &nbsp;Fred&nbsp; &nbsp;2&nbsp; &nbsp; &nbsp; 0&nbsp; &nbsp; &nbsp; &nbsp; 0&nbsp; &nbsp; &nbsp;1&nbsp; &nbsp; 0&nbsp; &nbsp; 0&nbsp; &nbsp;0&nbsp; &nbsp; &nbsp;1&nbsp; &nbsp; &nbsp;02&nbsp; &nbsp;Jeff&nbsp; &nbsp;3&nbsp; &nbsp; &nbsp; 0&nbsp; &nbsp; &nbsp; &nbsp; 1&nbsp; &nbsp; &nbsp;0&nbsp; &nbsp; 0&nbsp; &nbsp; 1&nbsp; &nbsp;0&nbsp; &nbsp; &nbsp;0&nbsp; &nbsp; &nbsp;13&nbsp; &nbsp; Amy&nbsp; &nbsp;4&nbsp; &nbsp; &nbsp; 0&nbsp; &nbsp; &nbsp; &nbsp; 0&nbsp; &nbsp; &nbsp;1&nbsp; &nbsp; 1&nbsp; &nbsp; 0&nbsp; &nbsp;0&nbsp; &nbsp; &nbsp;1&nbsp; &nbsp; &nbsp;04&nbsp; &nbsp;Mary&nbsp; &nbsp;5&nbsp; &nbsp; &nbsp; 0&nbsp; &nbsp; &nbsp; &nbsp; 1&nbsp; &nbsp; &nbsp;1&nbsp; &nbsp; 0&nbsp; &nbsp; 0&nbsp; &nbsp;1&nbsp; &nbsp; &nbsp;0&nbsp; &nbsp; &nbsp;0详细解释看这里 ->&nbsp;Pandas column of lists to separate columns
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python