字符串操作,然后将虚拟变量应用于它们

时间:2019-05-09 07:56:10

标签: python python-3.x pandas

我正在做一个小项目,以建立推荐系统。我有电影的类别列,我想在其上应用虚拟变量。

#Here is my data
movie_data.head()

Customer  MovieID  Movie_title       Movie_Category  Movie_cost($)

C1          M1     Gladiator         Drama,Action        3

C2          M2     The Accountant    Drama,Crime         2


#Applying the dummy variables
df_dummies = pd.get_dummies(movie_data['Movie_Category'])
df_dummies.head()

#Here's the output:
Drama,Action   Drama,Crime

1                   0

0                   1

#the desire output:
Drama   Action  Crime

1          1      0

1          0      1

1 个答案:

答案 0 :(得分:1)

使用Series.str.get_dummies

df_dummies = movie_data['Movie_Category'].str.get_dummies(',')