我正在处理电影数据并拥有电影流派的数据帧列。目前该列包含每部电影的电影流派列表(因为大多数电影都分配给多个流派),但为了进行此分析,我想解析列表并为每个流派创建一个新的数据帧列。因此,对于某部电影而言,不是有流派= ['戏剧','颤栗'],而是有两列,例如genre1 ='Drama'和genre2 ='Thriller'。在Pandas数据帧列中将列表拆分为多个列
这里是我的数据的一个片段:
{'color': {0: [u'Color::(Technicolor)'],
1: [u'Color::(Technicolor)'],
2: [u'Color::(Technicolor)'],
3: [u'Color::(Technicolor)'],
4: [u'Black and White']},
'country': {0: [u'USA'],
1: [u'USA'],
2: [u'USA'],
3: [u'USA', u'UK'],
4: [u'USA']},
'genre': {0: [u'Crime', u'Drama'],
1: [u'Crime', u'Drama'],
2: [u'Crime', u'Drama'],
3: [u'Action', u'Crime', u'Drama', u'Thriller'],
4: [u'Crime', u'Drama']},
'language': {0: [u'English'],
1: [u'English', u'Italian', u'Latin'],
2: [u'English', u'Italian', u'Spanish', u'Latin', u'Sicilian'],
3: [u'English', u'Mandarin'],
4: [u'English']},
'rating': {0: 9.3, 1: 9.2, 2: 9.0, 3: 9.0, 4: 8.9},
'runtime': {0: [u'142'],
1: [u'175'],
2: [u'202', u'220::(The Godfather Trilogy 1901-1980 VHS Special Edition)'],
3: [u'152'],
4: [u'96']},
'title': {0: u'The Shawshank Redemption',
1: u'The Godfather',
2: u'The Godfather: Part II',
3: u'The Dark Knight',
4: u'12 Angry Men'},
'votes': {0: 1793199, 1: 1224249, 2: 842044, 3: 1774083, 4: 484061},
'year': {0: 1994, 1: 1972, 2: 1974, 3: 2008, 4: 1957}}
任何帮助将不胜感激!谢谢!
没”甚至不知道t他是@jezrael建议的'add_prefix'选项。更可读。它会变成'pd.concat([df.drop(['genre'],axis = 1),df ['genre']。apply(pd.Series).add_prefix('genre _')],axis = 1) ' –
并且'apply(pd.Series)'很慢,最好不要用它。检查我的答案中的时间。 – jezrael
@jezrael感谢您好,以便将来使用! –