<p>IIUC,使用explode和itertuples,我们可以从数据帧创建元组</p>
<pre><code>s = df['genres'].str.split('|').explode().to_frame()
s['score'] = s.index.map(df['imdb_score'])
t = list(s.itertuples(index=False,name=None))
print(t)
[('Action', 7.9),
('Adventure', 7.9),
('Fantasy', 7.9),
('Sci-Fi', 7.9),
('Action', 7.1),
('Adventure', 7.1),
('Fantasy', 7.1),
('Action', 6.8),
('Adventure', 6.8),
('Thriller', 6.8),
('Action', 8.5),
('Thriller', 8.5),
('Documentary', 7.1),
('Comedy', 7.7),
('Drama', 7.7),
('Crime', 7.5),
('Drama', 7.5),
('Mystery', 7.5),
('Thriller', 7.5),
('Drama', 6.3),
('Horror', 6.3),
('Thriller', 6.3),
('Comedy', 6.3),
('Drama', 6.3),
('Romance', 6.3)]
</code></pre>
<p>如果需要以特定行为目标,则此函数使用<code>isin</code>将实现以下功能:</p>
<pre><code>def tuple_row(frame,row_num):
s = frame['genres'].str.split('|').explode().to_frame()
s['score'] = s.index.map(frame['imdb_score'])
return list(s[s.index.isin([row_num])].itertuples(index=False,name=None))
tuple_row(df,5)
[('Comedy', 7.7), ('Drama', 7.7)]
</code></pre>
<p>如果希望每一行都包含在嵌套的排序列表中</p>
<pre><code>l = [list(i.itertuples(name=None,index=False)) for _,i in s.groupby(level=0)]
[[('Action', 7.9), ('Adventure', 7.9), ('Fantasy', 7.9), ('Sci-Fi', 7.9)],
[('Action', 7.1), ('Adventure', 7.1), ('Fantasy', 7.1)],
[('Action', 6.8), ('Adventure', 6.8), ('Thriller', 6.8)],
[('Action', 8.5), ('Thriller', 8.5)],
[('Documentary', 7.1)],
[('Comedy', 7.7), ('Drama', 7.7)],
[('Crime', 7.5), ('Drama', 7.5), ('Mystery', 7.5), ('Thriller', 7.5)],
[('Drama', 6.3), ('Horror', 6.3), ('Thriller', 6.3)],
[('Comedy', 6.3), ('Drama', 6.3), ('Romance', 6.3)],
[('Documentary', 6.6)]]
</code></pre>