Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何根据数据帧中多个其他列的值添加两个新列?_Python_Pandas_Dataframe - Fatal编程技术网

Python 如何根据数据帧中多个其他列的值添加两个新列?

Python 如何根据数据帧中多个其他列的值添加两个新列?,python,pandas,dataframe,Python,Pandas,Dataframe,我试图根据其他几列的值向现有数据框添加两列。我的数据框如下所示: df=pd.DataFrame({'Type':['A','A','A','B','Type1':['A','A','B','B','Type2':['A','B','B','A','A','A','Score':[1,2,3,1,0,0],'Score1':[2,1,0,0],'Score2':[1,3,2,0] 我想添加两列“分数A”和“分数B”,这样“分数A”将是分数的平均值 对于类型为“A”的情况(每行)。“得分B”也是如

我试图根据其他几列的值向现有数据框添加两列。我的数据框如下所示:

df=pd.DataFrame({'Type':['A','A','A','B','Type1':['A','A','B','B','Type2':['A','B','B','A','A','A','Score':[1,2,3,1,0,0],'Score1':[2,1,0,0],'Score2':[1,3,2,0]

我想添加两列“分数A”和“分数B”,这样“分数A”将是分数的平均值 对于类型为“A”的情况(每行)。“得分B”也是如此。一个问题是,如果类型为空,则不应使用分数来计算平均值

在这种情况下,功能成功的结果将是:

Score_A  Score_B
  1.33      0 
  1.5       3
   3        2
   0        1
   2        0
   0        0
我已经在行级别运行了嵌套循环来实现这一点,但是有更好的方法吗

m1 = (df[['Type', 'Type1', 'Type2']] == 'A')
m2 = (df[['Type', 'Type1', 'Type2']] == 'B')
scores = df[['Score', 'Score1', 'Score2']]

df['Score_A'] = pd.DataFrame(np.where(m1, scores, np.nan)).mean(skipna=True, axis=1).fillna(0)
df['Score_B'] = pd.DataFrame(np.where(m2, scores, np.nan)).mean(skipna=True, axis=1).fillna(0)

print(df)
印刷品:

  Type Type1 Type2  Score  Score1  Score2   Score_A  Score_B
0    A     A     A      1       2       1  1.333333      0.0
1    A     A     B      2       1       3  1.500000      3.0
2    A           B      3       0       2  3.000000      2.0
3    B     B     B      1       1       1  0.000000      1.0
4                A      0       0       2  2.000000      0.0
5                       0       0       0  0.000000      0.0

这真漂亮+1.
  Type Type1 Type2  Score  Score1  Score2   Score_A  Score_B
0    A     A     A      1       2       1  1.333333      0.0
1    A     A     B      2       1       3  1.500000      3.0
2    A           B      3       0       2  3.000000      2.0
3    B     B     B      1       1       1  0.000000      1.0
4                A      0       0       2  2.000000      0.0
5                       0       0       0  0.000000      0.0