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

#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

最佳答案

使用Series.str.get_dummies

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

10-06 05:22