Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python - dataframe make columns representing vector

I have dataframe of genres

df = pd.DataFrame({'genres': [['Drama'], ['Music', 'Drama', 'Romance'],
                               ['Action', 'Adventure', 'Comedy'],
                               ['Thriller', 'Romance', 'Drama'],
                               ['Adventure', 'Family']]
                    })
print(df)
genres = ['Action', 'Adventure', 'Comedy', 'Drama', 'Family', 'Music', 'Romance', 'Thriller']  # list of all genres

data:

                        genres
0                      [Drama]
1      [Music, Drama, Romance]
2  [Action, Adventure, Comedy]
3   [Thriller, Romance, Drama]
4          [Adventure, Family]

i want to have output like:

                        genres  Action  Adventure  Comedy  Drama  Family  \
0                      [Drama]       0          0       0      1       0   
1      [Music, Drama, Romance]       0          0       0      1       0   
2  [Action, Adventure, Comedy]       1          1       1      0       0   
3   [Thriller, Romance, Drama]       0          0       0      1       0   
4          [Adventure, Family]       0          1       0      0       1   

   Music  Romance  Thriller  
0      0        0         0  
1      1        1         0  
2      0        0         0  
3      0        1         1  
4      0        0         0  
like image 705
Nihal Avatar asked Apr 11 '26 12:04

Nihal


1 Answers

Use MultiLabelBinarizer:

from sklearn.preprocessing import MultiLabelBinarizer

mlb = MultiLabelBinarizer()

df1 = pd.DataFrame(mlb.fit_transform(df['genres']),columns=mlb.classes_, index=df.index)
df = df.join(df1)
print (df)
                        genres  Action  Adventure  Comedy  Drama  Family  \
0                      [Drama]       0          0       0      1       0   
1      [Music, Drama, Romance]       0          0       0      1       0   
2  [Action, Adventure, Comedy]       1          1       1      0       0   
3   [Thriller, Romance, Drama]       0          0       0      1       0   
4          [Adventure, Family]       0          1       0      0       1   

   Music  Romance  Thriller  
0      0        0         0  
1      1        1         0  
2      0        0         0  
3      0        1         1  
4      0        0         0  

If want filter genres by lists add reindex:

genres = ['Action', 'Adventure', 'Comedy', 'Drama']

df1 = pd.DataFrame(mlb.fit_transform(df['genres']),columns=mlb.classes_, index=df.index)
df = df.join(df1.reindex(columns=genres, fill_value=0))
print (df)
                        genres  Action  Adventure  Comedy  Drama
0                      [Drama]       0          0       0      1
1      [Music, Drama, Romance]       0          0       0      1
2  [Action, Adventure, Comedy]       1          1       1      0
3   [Thriller, Romance, Drama]       0          0       0      1
4          [Adventure, Family]       0          1       0      0
like image 115
jezrael Avatar answered Apr 14 '26 01:04

jezrael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!