Python Panda: Find A Specific String In A Column And Fill The Column Matching The String
I have a dataframe with several columns. One of them is filled with 'genres' of movie separated by |, I've splitted this column in several others to get X columns each filled with
Solution 1:
I think you need pop
for extract column with str.get_dummies
and join
to original:
df = pd.DataFrame({'A': ['Drama|Action', 'Drama', 'Action'], 'B':range(3)},
index = ['a1', 'a2', 'a3'])
print (df)
A B
a1 Drama|Action 0
a2 Drama 1
a3 Action 2
df = df.join(df.pop('A').str.get_dummies())
print (df)
B Action Drama
a1 0 1 1
a2 1 0 1
a3 2 1 0
If want original column:
df = df.join(df['A'].str.get_dummies())
print (df)
A B Action Drama
a1 Drama|Action 0 1 1
a2 Drama 1 0 1
a3 Action 2 1 0
Post a Comment for "Python Panda: Find A Specific String In A Column And Fill The Column Matching The String"