Python 将值替换为NaN';如果其他矩阵值等于表中的某个值

Python 将值替换为NaN';如果其他矩阵值等于表中的某个值,python,pandas,replace,nan,missing-data,Python,Pandas,Replace,Nan,Missing Data,我有两个多索引数据帧;1表示哪个玩家在服务器上,另一个跟踪分数。在此,发球球员轮换每场比赛 col0 = ['Game 1','Game 1','Game 2','Game 2','Game 3','Game 3','Game 4','Game 4','Game 5','Game 5'] col1 = ['P1','P2','P1','P2','P1','P2','P1','P2','P1','P2'] a = pd.DataFrame(data = np.random.rand(3,10))

我有两个多索引数据帧;1表示哪个玩家在服务器上,另一个跟踪分数。在此,发球球员轮换每场比赛

col0 = ['Game 1','Game 1','Game 2','Game 2','Game 3','Game 3','Game 4','Game 4','Game 5','Game 5']
col1 = ['P1','P2','P1','P2','P1','P2','P1','P2','P1','P2']
a = pd.DataFrame(data = np.random.rand(3,10))
a.columns = [col0,col1]

     Game 1              Game 2  ...    Game 4    Game 5          
         P1        P2        P1  ...        P2        P1        P2
0  0.375562  0.408865  0.107393  ...  0.552553  0.986619  0.635726
1  0.101053  0.949870  0.804260  ...  0.895951  0.384401  0.368055
2  0.879938  0.740631  0.369314  ...  0.624967  0.061308  0.625157
数据框“b”指示哪个球员发球

col0 = ['Game 1','Game 2','Game 3','Game 4','Game 5']
col1 = ['Server','Server','Server','Server','Server']
b = pd.DataFrame([[1,2,1,2,1],
                  [2,1,2,1,2], 
                  [1,2,1,2,1]])
b.columns = [col0, col1] 

  Game 1 Game 2 Game 3 Game 4 Game 5
  Server Server Server Server Server
0      1      2      1      2      1
1      2      1      2      1      2
2      1      2      1      2      1 
现在我想创建dataframe c,它看起来像:

     Game 1              Game 2  ...    Game 4    Game 5          
         P1        P2        P1  ...        P2        P1        P2
0  0.375562  0.408865  np.nan    ...  np.nan    0.986619  0.635726
1  np.nan    np.nan    0.804260  ...  0.895951  np.nan    np.nan
2  0.879938  0.740631  np.nan    ...  np.nan    0.061308  0.625157
我希望在球员2发球时,数据帧“a”的值被NaN的值替换。例如,在数据帧“c”的第一行中,仅显示游戏1、游戏3和游戏5中的点数,因为玩家1在这些游戏中发球


什么都行

您可以尝试使用
reindex
replace
where

选项1

temp=b.reindex(columns=map(lambda x:(x[0],'Server') ,a.columns)).replace({1:True,2:False})
a.where(temp.values)

np相同。其中

选项2

import numpy as np
temp=b.reindex(columns=map(lambda x:(x[0],'Server') ,a.columns))
pd.DataFrame(np.where(temp.eq(1), a, np.nan),columns=a.columns)

与修改原始b相同,并使用
应用遮罩,其中

选项3

msk=[x.repeat(2)==1 for x in b.values]
a.where(msk)


选项1的详细信息:

首先映射
a
的列,如下所示:

list(map(lambda x:(x[0],'Server') ,a.columns))
[('Game 1', 'Server'), ('Game 1', 'Server'), ('Game 2', 'Server'), ('Game 2', 'Server'), ('Game 3', 'Server'), ('Game 3', 'Server'), ('Game 4', 'Server'), ('Game 4', 'Server'), ('Game 5', 'Server'), ('Game 5', 'Server')] 
然后将
reindex
与该映射列表一起使用:

b.reindex(columns=map(lambda x:(x[0],'Server') ,a.columns))
  Game 1        Game 2        Game 3        Game 4        Game 5       
  Server Server Server Server Server Server Server Server Server Server
0      1      1      2      2      1      1      2      2      1      1
1      2      2      1      1      2      2      1      1      2      2
2      1      1      2      2      1      1      2      2      1      1 
之后,使用
replace
获取
temp
的更改值:

b.reindex(columns=map(lambda x:(x[0],'Server') ,a.columns)).replace({1:True,2:False})
  Game 1        Game 2        Game 3        Game 4        Game 5       
  Server Server Server Server Server Server Server Server Server Server
0   True   True  False  False   True   True  False  False   True   True
1  False  False   True   True  False  False   True   True  False  False
2   True   True  False  False   True   True  False  False   True   True 
最后,使用
where
和这个掩码(
temp
)映射
a
的值:

a.where(temp.values)
     Game 1             Game 2              Game 3              Game 4  \
         P1       P2        P1        P2        P1        P2        P1   
0  0.973453  0.02111       NaN       NaN  0.435252  0.335656       NaN   
1       NaN      NaN  0.195463  0.960642       NaN       NaN  0.527152   
2  0.280339  0.97697       NaN       NaN  0.833331  0.476428       NaN   

               Game 5            
         P2        P1        P2  
0       NaN  0.676733  0.600626  
1  0.924126       NaN       NaN  
2       NaN  0.675638  0.319161  

好东西伙计+1谢谢你伙计:)@wwnde