我想将旧矩阵中的填充值删除到新矩阵中。 python用什么语法或代码来做到这一点?
这是我想要的从旧矩阵到最终矩阵的矩阵。 如何在python中做到这一点?有什么语法或一些代码可以删除最终矩阵中新的旧值?
here's the final result I want to be
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 |
答案 0 :(得分:0)
也许这会有所帮助:
# 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