python - 如何删除新矩阵中的某些矩阵的旧值
问题描述
我想将旧矩阵中的填充值删除到我的新矩阵中。python中有什么语法或代码来做到这一点?
这是从旧矩阵到我想要的最终矩阵的矩阵。如何在python中做到这一点?是否有任何语法或一些代码可以删除新最终矩阵中的旧值?
import numpy as np
import pandas as pd
from pandas import read_csv
read = pd.read_csv('rating_trainingreg.csv')
ratings_df = pd.pivot_table(raw_dataset_df, index='User_ID',
columns='Training_ID',
aggfunc=np.max)
predicted_ratings_df = pd.DataFrame(index=ratings_df.index,
columns=ratings_df.columns,
data=predicted_ratings)
reviewed_training_df = reviewed_training_df.join(training_df,
on='Training_ID')
already_reviewed = reviewed_training_df['Training_ID']
newMatrix = predicted_ratings_df.index,isin(already_reviewed) == False
newMatrix
OLD matirx:
Rating
TrainingID| 1001 | 1002 | 1003 | 1004 |
UserID | | | | 2 |
1 | 2 | | | 3 |
2 | | 3 | | |
3 | 2 | | | |
4 | | | | |
After some calculation:
Rating
TrainingID| 1001 | 1002 | 1003 | 1004 |
UserID | 1 | 2 | 2 | 2 |
1 | 2 | 3 | 3 | 3 |
2 | 3 | 3 | 4 | 2 |
3 | 2 | 1 | 4 | 2 |
4 | 4 | 1 | 5 | 2 |
Final matrix that I want:
Rating
TrainingID| 1001 | 1002 | 1003 | 1004 |
UserID | 1 | 2 | 2 | |
1 | | 3 | 3 | |
2 | 3 | | 4 | 2 |
3 | | 1 | 4 | 2 |
4 | 4 | 1 | 5 | 2 |
解决方案
也许这有帮助:
# manual way
newMatrix[2005][1001] = None
newMatrix[2002][1002] = None
newMatrix[2003][1004] = None
newMatrix[2005][1005] = None
newMatrix[2007][1007] = None
现在,正确的方法是,如果您有一个很长的矩阵:
# iterate the old matrix to set None in the new matrix where appropriate
for i, j in oldMatrix.iterrows():
if oldMatrix[i][j]:
newMatrix[i][j] = None
推荐阅读
- reactjs - 使用条件 const 反应调用函数并返回
- c# - 如何在类中缓存查询结果,然后根据 C# 方法中传递的参数重用它进行搜索?
- python - 格式化单行 XML 的问题
- javascript - Javascript 将所有 DOM 元素打印/可视化为保持其结构的节点树
- tensorflow - Tensorflow:累积张量的梯度
- android - Android:如何用字符换行?(不是用语言)帮助我
- itfoxtec-identity-saml2 - 如何将用户名/电子邮件提供给身份提供商以预先填写在 IdP 的用户名框中?
- javascript - WordPress REST API Ajax 显示更多帖子按钮
- python - TensorFlow 2.0 tf.keras API Eager 模式与 Graph 模式
- scrapy - evaljs 返回 nil 即使它不应该?