简体   繁体   English

将Numpy矩阵转换为熊猫数据框

[英]Convert Numpy matrix to pandas dataframe

Given a rating matrix in .dat: 给定.dat中的评分矩阵:

rating = np.load(os.path.join(data_dir, "rating.dat"))    

matrix([[ 5,  4,  0, 0],
        [ 0,  0,  5, 0],
        [ 0,  0,  0, 1],
        [ 0,  0,  0, 1]])

And a df such as: 和df之类的:

df=pd.read_csv('data_path')

df

   user     item
0  foo      qw   
1  foo      rt
2  coo      ty
3  doo      yu
4  moo      yu

The rating matrix row corresponds user and column item and values are ratings. 评分矩阵行对应于user和列item ,值是评分。 I want to add this matrix to my df as an additional column, in order to have a result like this: 我想将此矩阵添加到我的df作为附加列,以得到如下结果:

   user     item    rating
0  foo      qw      5
1  foo      rt      4
2  coo      ty      5
3  doo      yu      1
4  moo      yu      1 

Thank you in advance! 先感谢您!

Given a rating matrix: 给定一个评分矩阵:

ratings = np.asarray([
    [ 5,  4,  0, 0],
    [ 0,  0,  5, 0],
    [ 0,  0,  0, 1],
    [ 0,  0,  0, 1]
])
ratings.flatten()[ratings.flatten().nonzero()]
Out[1]: array([5, 4, 5, 1, 1])

The trick is to flatten the matrix and remove the non-zero elements. 诀窍是使矩阵变平并删除非零元素。 Then simply df['ratings'] = ratings and you will have your column filled in the proper order. 然后只需df['ratings'] = ratings ,您就可以按正确的顺序填充列。 Note that if some user makes several reviews, also has several rows in your df . 请注意,如果某位用户进行了多条评论,则df也会有几行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM