【问题标题】:How to put element into multiple categories in python/pandas?如何在 python/pandas 中将元素放入多个类别?
【发布时间】:2021-09-23 11:06:13
【问题描述】:

例如,

input.csv如下:

Song Name Genre
7 Rings 'dance pop', 'pop', 'post-teen pop'
Run 'dance pop', 'piano rock', 'pop', 'pop rock'
Dance Monkey 'australian pop', 'pop'
All Of Me 'neo soul', 'pop', 'pop soul', 'r&b', 'urban contemporary'

我想以一种我可以得到类似的方式对其进行分组:

pop: ['7 Rings', 'Run', 'Dance Monkey', 'All Of Me']
dance pop : ['7 Rings','Run']
r&b: ['All Of Me']

甚至将其放入另一个表/数据框/csv 中,例如:

pop dance pop r&b neo soul pop rock
7 Rings 7 Rings All Of Me All Of Me Run
Run Run
Dance Monkey
All Of Me

有没有办法做到这一点?

编辑:

尝试了 mozway 的建议,我得到了一个看起来像这样的表格:

genreExplode=df.explode('Genre').assign(index=lambda d: d.groupby('Genre').cumcount()).pivot(index='index', columns='Genre', values='Song Name').fillna('')
genreExplode.head()
Genre 'dance pop', 'pop', 'post-teen pop' 'dance pop', 'piano rock', 'pop', 'pop rock' 'australian pop', 'pop' 'neo soul', 'pop', 'pop soul', 'r&b', 'urban contemporary'
index
0 7 Rings Run Dance Monkey All Of Me

编辑 2:

找出问题所在,Genre 列中的对象看起来像列表,但实际上是字符串。

genrelist=df['Genre'].tolist() ##first make a list of the Genre column
genrelist_new=[] ## new list to hold lists

import ast ## found this online
for x in genrelist:
    x=ast.literal_eval(x) ##this loop takes the string objects that look like list in genrelist and converts them into list
    genrelist_new.append(x) ##then add the converted list and put into a list

df['Genre']=genrelist_new ##replace old Genre column of strings that look like lists to new column of real lists
genreExplode=spotData.explode('Genre').assign(index=lambda d: d.groupby('Genre').cumcount()).pivot(index='index', columns='Genre', values='Song Name').fillna('')
genreExplode.head() ## this result is what I was looking for!

解决办法,将字符串转换成真实的列表,这样Genre列就是列表的列表。

那么@mozway 的代码可以完美运行。

【问题讨论】:

  • 原始 csv 列中的流派也放在方括号中,如 ['pop','rock','dance'],但我无法显示它,因为它会触发我的警告即使不是代码,代码也没有正确格式化。
  • 考虑将您的解决方案放在答案中,以便将问题标记为已解决。

标签: python pandas jupyter


【解决方案1】:

假设“流派”包含列表(例如['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                                                                

【讨论】:

  • 我复制并运行了你的代码,不确定我是否遗漏了什么,但我得到了每首歌曲名称只出现一次的表格,并且列标题是整个流派列表,而不是单个流派列表。
  • @RiBoFen 我没有得到完整的评论,请在问题中发布代码/输出
  • @Corralien 幸运的是我让你注意我的错别字;)
  • 我已经更新了我的帖子
  • 我想我知道它为什么不起作用,我认为流派列中的对象不会被识别为列表,即使它们看起来像方括号中的列表,所以也许我需要先把类型改成列表?
猜你喜欢
  • 2013-11-17
  • 1970-01-01
  • 2023-03-31
  • 1970-01-01
  • 2020-12-16
  • 1970-01-01
  • 1970-01-01
  • 2017-10-25
  • 2019-09-09
相关资源
最近更新 更多