IIU C, используя explode и itertuples, мы можем создать кортеж из фрейма данных.
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)]
, если вам нужно нацелиться на указанную строку c, тогда эта функция с помощью isin
сделает хитрость:
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)]
, если вы хотите, чтобы каждая строка во вложенном списке сортировок.
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)]]