Reputation: 1433
How to preprocess multivalued feature (genre_ids)?
As I need to assign one numeric value to each row of one column, but how efficiently I can preprocess this genre_ids?
msno |city |bd |gender| song_id| song_length| genre_ids| artist_name
1 12 33 0 11 222 372 89
2 11 23 1 1 202 372|374|375 99
3 14 22 1 2 300 386 78
Upvotes: 0
Views: 48
Reputation: 3308
You can use pandas package to solve your task:
import pandas as pd
df = pd.DataFrame({'msno': [1, 2, 3],
'city': [12, 11, 14],
'bd': [33, 23, 22],
'gender': [0, 1, 1],
'song_id': [11, 1, 2],
'song_length': [222, 202, 300],
'genre_ids': ['372', '372|374|375', '386'],
'artist_name': [89, 99, 78]})
genre_ids_dummies = (pd.get_dummies(df.genre_ids.apply(lambda x: x.split('|'))
.apply(pd.Series)
.stack()).sum(level=0))
df_processed = pd.concat(objs=[df.drop(labels='genre_ids', axis=1),
genre_ids_dummies], axis=1)
Upvotes: 1