![](/img/trans.png)
[英]Pandas sparse dataFrame to sparse matrix, without generating a dense matrix in memory
[英]How do I convert a sparse matrix in a pandas dataframe to a dense one?
我有一個(userId, movieId, rating)
其中包含(userId, movieId, rating)
格式的行。 UserIds 和 movieIds 是任意字符串。
我想將它轉換為維度的密集矩陣(# of users, # of movies)
,如果該行存在於第一行中,則每個單元格都進行rating
,否則為0
。
我怎么能在大熊貓/ numpy 中做到這一點?
我想我設法通過使用pivot()
來做到這一點,如下所示:
matrix.pivot(index='userId', columns='movieId', values='rating').fillna(0.0)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.