首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将类别在我的dataframe中的一列中分离

将类别在我的dataframe中的一列中分离
EN

Stack Overflow用户
提问于 2019-09-20 18:38:05
回答 1查看 1.2K关注 0票数 0

我需要研究一下什么是最具成本效益的电影类型。我的问题是,这些类型都是在一个字符串中提供的:

这给了我大约300个不同的类别。我如何把它们分成12个原始的虚拟体裁专栏,这样我就可以分析每一个主要的体裁。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-21 12:46:23

感谢王勇,他提出了熊猫的get_dummies功能。我们可以大大缩短代码:

代码语言:javascript
复制
df = pd.DataFrame({
    'movie_id': range(5),
    'gernes': [
                'Action|Adventure|Fantasy|Sci-Fi',
                'Action|Adventure|Fantasy',
                'Action|Adventure|Thriller',
                'Action|Thriller',
                'Action|Adventure|Sci-Fi'
              ]
})  
dummies = df['gernes'].str.get_dummies(sep='|')
final = pd.concat([df, dummies], axis=1)

结果:

代码语言:javascript
复制
   movie_id                           gernes  Action  Adventure  Fantasy  Sci-Fi  Thriller
0         0  Action|Adventure|Fantasy|Sci-Fi       1          1        1       1         0
1         1         Action|Adventure|Fantasy       1          1        1       0         0
2         2        Action|Adventure|Thriller       1          1        0       0         1
3         3                  Action|Thriller       1          0        0       0         1
4         4          Action|Adventure|Sci-Fi       1          1        0       1         0

原始答案

一种结合熊猫和机器学习数据准备技术的解决方案。假设你用的是熊猫v0.25或更高的版本。

首先,让我们从屏幕截图中创建一个dataframe:

代码语言:javascript
复制
df = pd.DataFrame({
    'movie_id': range(5),
    'gernes': [
                'Action|Adventure|Fantasy|Sci-Fi',
                'Action|Adventure|Fantasy',
                'Action|Adventure|Thriller',
                'Action|Thriller',
                'Action|Adventure|Sci-Fi'
              ]
})

   movie_id                           gernes
0         0  Action|Adventure|Fantasy|Sci-Fi
1         1         Action|Adventure|Fantasy
2         2        Action|Adventure|Thriller
3         3                  Action|Thriller
4         4          Action|Adventure|Sci-Fi

一部电影可以属于多种细菌。我们想要的是通过一个叫做一热编码的过程来分离这些细菌。我们定义的类别(行动,冒险,颤栗等)并将每部电影标记为属于或不属于每一类别:

代码语言:javascript
复制
from sklearn.preprocessing import OneHotEncoder

s = df['gernes'].str.split('|').explode()
encoder = OneHotEncoder()
encoded = encoder.fit_transform(s.values[:, None])
one_hot_df = pd.DataFrame(encoded.toarray(), columns=np.ravel(encoder.categories_), dtype='int') \
                .groupby(s.index) \
                .sum()

   Action  Adventure  Fantasy  Sci-Fi  Thriller
0       1          1        1       1         0
1       1          1        1       0         0
2       1          1        0       0         1
3       1          0        0       0         1
4       1          1        0       1         0

这意味着第一部电影属于“行动”、“冒险”、“幻想”和“科幻”,而不是“颤栗”类,第二部属于“行动”、“冒险与幻想”等等。最后一站是把它们结合在一起:

代码语言:javascript
复制
final = pd.concat([df, one_hot_df], axis=1)

   movie_id                           gernes  Action  Adventure  Fantasy  Sci-Fi  Thriller
0         0  Action|Adventure|Fantasy|Sci-Fi       1          1        1       1         0
1         1         Action|Adventure|Fantasy       1          1        1       0         0
2         2        Action|Adventure|Thriller       1          1        0       0         1
3         3                  Action|Thriller       1          0        0       0         1
4         4          Action|Adventure|Sci-Fi       1          1        0       1         0
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58033652

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档