如何将元素放入python/pandas中的多个类别?



例如

input.csv如下:

tbody> <<tr>
歌名 流派
7环"舞蹈流行","流行"、"post-teen流行">
"舞蹈流行","钢琴摇滚"、"流行","流行摇滚">
舞蹈猴子"澳大利亚流行"、"流行">
我的一切"neo的灵魂","流行"、"流行的灵魂","r& b","城市当代">

假设"包含列表(),例如['dance pop', 'pop', 'post-teen pop'])。explodepivot:

(df.explode('Genre')
.assign(index=lambda d: d.groupby('Genre').cumcount())
.pivot(index='index', columns='Genre', values='Song')
.fillna('')
)

输出:

Genre australian pop dance pop   neo soul piano rock           pop pop rock   pop soul post-teen pop        r&b urban contemporary
index                                                                                                                             
0       Dance Monkey   7 Rings  All Of Me        Run       7 Rings      Run  All Of Me       7 Rings  All Of Me          All Of Me
1                          Run                                 Run                                                                
2                                                     Dance Monkey                                                                
3                                                        All Of Me                                                                

最新更新