猿问

以字典的形式表示数据集:key = (movie_title, movieId)

我正在寻找一种方法,可以让我以字典的形式表示我的数据集:key = (movie_title, movieId) value = array


这是我的数据帧:


movie_title    movieId    Action   Adventure  Fantasy   Sci-Fi.  Thriller

Avatar            1        1.0       1.0        1.0      1.0       0.0

John Carter       2        1.0       1.0        0.0      1.0       0.0  

Tangled           3        0.0       1.0        1.0      0.0       0.0  

我的数组是:


df_array = userGenreTable.as_matrix(columns=userGenreTable.columns[2:])

我用了 :


userGenreTable.to_dict('records')

但这不是我要找的。


我知道有方法:df.keys(),df.iterrows()


但这不是我要找的。


慕妹3242003
浏览 257回答 2
2回答

白猪掌柜的

用 -df.set_index(df[['movie_title','movieId']].apply(tuple,axis=1))[['Action','Adventure','Fantasy','Sci-Fi.','Thriller']].T.to_dict('list')输出{('Avatar', 1): [1.0, 1.0, 1.0, 1.0, 0.0], ('John_Carter', 2): [1.0, 1.0, 0.0, 1.0, 0.0], ('Tangled', 3): [0.0, 1.0, 1.0, 0.0, 0.0]}

拉风的咖菲猫

使用set_index和tolist:df.set_index(['movie_title','movieId'],inplace=True)dict(zip(df.index.tolist(),df.values.tolist())){('Avatar', 1): [1.0, 1.0, 1.0, 1.0, 0.0], ('John Carter', 2): [1.0, 1.0, 0.0, 1.0, 0.0], ('Tangled', 3): [0.0, 1.0, 1.0, 0.0, 0.0]}
随时随地看视频慕课网APP

相关分类

Python
我要回答