python - 使用 pandas 将单列编码为多列

标签 python pandas dataframe

我在 pandas 数据框中有一列用于流派。它是一个由列分隔的流派字符串。

>>> df['genres_omdb']
0                      Crime, Drama
1        Adventure, Family, Fantasy
2                    Drama, Mystery
3         Horror, Mystery, Thriller
5         Action, Adventure, Sci-Fi
6                    Drama, Romance
8                             Drama
9      Animation, Adventure, Comedy
10     Animation, Adventure, Comedy
11                    Drama, Sci-Fi
12                            Drama
13              Drama, Romance, War
14            Comedy, Drama, Family
16         Comedy, Musical, Romance

所以最初我将其分为三列,并在每列上运行 get_dummies。这产生了重复的列(即流派1_冒险流派2_冒险)。

然后,我尝试获取每种独特的流派,创建该流派的一列,然后手动迭代行,如果该流派在列表中,则将值更改为 1。

genre1_keys = df['genre1'].value_counts().keys()
genre2_keys = df['genre2'].value_counts().keys()
genre3_keys = df['genre3'].value_counts().keys()
for genre in genre1_keys:
  all_genres.add(genre.strip())
for genre in genre2_keys:
  all_genres.add(genre.strip())
for genre in genre3_keys:
  all_genres.add(genre.strip())
for genre in all_genres:
  df[genre] = 0
for i, row in df.iterrows():
  genres = row['genres_omdb'].split(',')
  for genre in genres:
    genre = genre.strip()
    row[genre] = 1

这非常困惑,我知道有更好的方法来做到这一点。任何有关如何清理此代码的帮助将不胜感激。

最佳答案

我认为你只需要str.get_dummies

df['genres_omdb'].str.get_dummies(sep=',')
Out[115]: 
    Action  Adventure  Animation  Comedy  Crime  Drama  Family  Fantasy  \
0        0          0          0       0      1      1       0        0   
1        0          1          0       0      0      0       1        1   
2        0          0          0       0      0      1       0        0   
3        0          0          0       0      0      0       0        0   
5        1          1          0       0      0      0       0        0   
6        0          0          0       0      0      1       0        0   
8        0          0          0       0      0      1       0        0   
9        0          1          1       1      0      0       0        0   
10       0          1          1       1      0      0       0        0   
11       0          0          0       0      0      1       0        0   
12       0          0          0       0      0      1       0        0   
13       0          0          0       0      0      1       0        0   
14       0          0          0       1      0      1       1        0   
16       0          0          0       1      0      0       0        0   
    Horror  Musical  Mystery  Romance  Sci-Fi  Thriller  War  
0        0        0        0        0       0         0    0  
1        0        0        0        0       0         0    0  
2        0        0        1        0       0         0    0  
3        1        0        1        0       0         1    0  
5        0        0        0        0       1         0    0  
6        0        0        0        1       0         0    0  
8        0        0        0        0       0         0    0  
9        0        0        0        0       0         0    0  
10       0        0        0        0       0         0    0  
11       0        0        0        0       1         0    0  
12       0        0        0        0       0         0    0  
13       0        0        0        1       0         0    1  
14       0        0        0        0       0         0    0  
16       0        1        0        1       0         0    0  

关于python - 使用 pandas 将单列编码为多列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48794354/

相关文章:

python - 偏移量前滚后加上一个月偏移量后的 Pandas 超出纳秒时间戳

python - 如何在 Pandas 数据框中用不同颜色为 bool 值着色

python - 3rd 方库和 Py2exe

python - .astype ("int") 或 .astype(int)?有和没有引号/双引号之间有什么区别吗?

python - Pandas GroupBy 均值

r - 如何找到到最近的非重叠元素的距离?

Python Pandas : update dataframe values from another dataframe

python:无法从用户定义的函数返回 pandas 数据框(可能是用户错误)

python - 如何使用 python 将列表理解结果传递到 csv

python - 使用 pandas read_html 抓取时将表行分隔为 2