Как создать функцию, которая позволит мне взять первую строку кадра данных и создать список двумерных кортежей? - PullRequest
1 голос
/ 21 апреля 2020
 df100=a[['genres','imdb_score']]
 df100
            genres                imdb_score
 0  Action|Adventure|Fantasy|Sci-Fi 7.9
 1  Action|Adventure|Fantasy        7.1
 2  Action|Adventure|Thriller       6.8
 3  Action|Thriller                 8.5
 4  Documentary                     7.1
       ...  ... ...
5038 Comedy|Drama                   7.7
5039 Crime|Drama|Mystery|Thriller   7.5
5040 Drama|Horror|Thriller          6.3
5041 Comedy|Drama|Romance           6.3
5042 Documentary                    6.6

def tuples(p):

   t= [(p[0], p[1]) for p[0], p[1] in zip(df100.genres, df100.imdb_score) for p[0] in p[0].split('|')]

     return t

tuples(df100.loc[0,['genres','imdb_score']])

Итак, я создал приведенный выше кадр данных с жанрами и счетом imdb в виде столбцов. Затем я создал функцию tuples (), в которой столбец жанра разделяет каждый уникальный жанр, а затем добавляет imdb_score рядом с ним (как показано ниже). Затем я применил эту функцию, показанную как tuples (df100.loc [0, [ 'genres', 'imdb_score']]), в надежде получить то, что видно ниже, 2d кортежи только в первой строке кадра данных. Однако в итоге я получил один полный список для всех строк в кадре данных, а не только для первой строки. Может ли кто-нибудь помочь в том, как я могу изменить функцию, чтобы я использовал ее в первой строке, а затем применил ее ко всему фрейму данных, отдельно.

[('Action',7.9),('Adventure',7.9),('Fantasy',7.9),('Sci-Fi',7.9)]

Ответы [ 3 ]

1 голос
/ 21 апреля 2020
import pandas as pd
from datetime import datetime


def get_tuples(p):    
    t = [(k, p['imdb_score']) for k in p['genres'].split('|')]

    return t


df100 = pd.DataFrame({'genres': ['Action|Adventure|Fantasy|Sci-Fi', 'Action|Adventure|Fantasy', 'Action|Adventure|Thriller'],
                   'imdb_score': [7.9, 7.1, 6.8]})

x = get_tuples(df100.loc[0, ['genres','imdb_score']])

print(x)

Выход:

[('Action', 7.9), ('Adventure', 7.9), ('Fantasy', 7.9), ('Sci-Fi', 7.9)]
0 голосов
/ 21 апреля 2020

IIU C, вы хотите список кортежей для каждой строки; Вы можете добиться этого, используя zip и itertools product . Вы должны быть в состоянии изменить его так, как вам нужно. дай мне знать, если мое предположение неверно:

from itertools import product
                 #create a cross join with product 
                 #entry from df.imdb_score is wrapped in a list
                 #else the string will be used and the cross join
                 #will produce a combo of individual strings and genre
df['pairing'] = [list(product(genre,[score]))
                 for genre,score in
                  #split the data, before pairing 
                 zip([ent.split('|') for ent in df.genres],df.imdb_score)]
df.head()
         genres                     imdb_score  pairing
0   Action|Adventure|Fantasy|Sci-Fi 7.9 [(Action, 7.9), (Adventure, 7.9), (Fantasy, 7....
1   Action|Adventure|Fantasy        7.1 [(Action, 7.1), (Adventure, 7.1), (Fantasy, 7.1)]
2   Action|Adventure|Thriller       6.8 [(Action, 6.8), (Adventure, 6.8), (Thriller, 6...
3   Action|Thriller                 8.5 [(Action, 8.5), (Thriller, 8.5)]
4   Documentary                     7.1 [(Documentary, 7.1)]
0 голосов
/ 21 апреля 2020

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)]]
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...