如何创建允许我使用此数据框创建2d元组列表的函数?

时间:2020-04-16 17:00:12

标签: python pandas dataframe tuples

 df1=a[['genres','imdb_score']]
 df1


        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):
list=[]
t=df1.genres.str.split('|').join(df1['imdb_score'])
list.append(t)
return list

这是我所做的,但是没有用,我想在下面得到内容。非常感谢。

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

1 个答案:

答案 0 :(得分:1)

以下功能可以满足您的需求:

def get_columns_as_tuples(df, indices):
    return [[(genre, score) for 
              genre in genres.split('|')] for 
              _, (genres, score) in df.loc[indices].iterrows()]

它返回列表列表,因为它允许您选择要从中获取数据的索引。

示例:

# get data from rows whose index is in [0]
>>> get_columns_as_tuples(df=df, indices=[0])
[[('Action', 7.9), ('Adventure', 7.9), ('Fantasy', 7.9), ('Sci-Fi', 7.9)]]

# get data from rows whose index is in [0, 1]
>>> get_columns_as_tuples(df=df, indices=[0, 1])
[[('Action', 7.9), ('Adventure', 7.9), ('Fantasy', 7.9), ('Sci-Fi', 7.9)],
 [('Action', 7.1), ('Adventure', 7.1), ('Fantasy', 7.1)]]
相关问题