我想找出特定分类的变量的SUM。我的变量a是(CM)分类,另一个是float(zahlgesnet)。我遇到错误,我无法真正意识到自己的原因。
这实际上是为了计算混淆矩阵输出的总和。到目前为止,我尝试使用其他方法,但是我想知道为什么该方法不起作用。由于我是python的初学者,所以不确定此方法是否总体合适。
这是数据的样子:
ID zahlgesnet CM
1 1 2.234,42 0
3 2 0 3
4 3 234 0
6 4 8.234 2
7 5 653,23 1
9 6 134 2
这是我的代码:
SummeFF = 0
SummeFT = 0
SummeTF = 0
SummeTT = 0
result = 0
def getsums(x,y,z,v):
for i in range(len(X_valid)):
if X_valid.CM[i] == 0:
x += (X_valid.zahlgesnet[i])
elif X_valid.CM[i] == 1:
y += (X_valid.zahlgesnet[i])
elif X_valid.CM[i] == 2:
z += (X_valid.zahlgesnet[i])
elif X_valid.CM[i] == 3:
v += (X_valid.zahlgesnet[i])
getsums(SummeFF,SummeFT,SummeTF,SummeTT)
print(SummeFF,SummeFT,SummeTF,SummeTT)
基于分类器CM,我希望该函数可以遍历所有值并给出四个不同的和。
我得到的错误是:
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-40-788203eee12b> in <module>
20
21
---> 22 getsums(SummeFF,SummeFT,SummeTF,SummeTT)
23 print(SummeFF,SummeFT,SummeTF,SummeTT)
24
<ipython-input-40-788203eee12b> in getsums(x, y, z, v)
7 def getsums(x,y,z,v):
8 for i in range(len(X_valid)):
----> 9 if X_valid.CM[i] == 0:
10 x += (X_valid.zahlgesnet[i])
11 elif X_valid.CM[i] == 1:
~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\series.py in __getitem__(self, key)
1066 key = com.apply_if_callable(key, self)
1067 try:
-> 1068 result = self.index.get_value(self, key)
1069
1070 if not is_scalar(result):
~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_value(self, series, key)
4728 k = self._convert_scalar_indexer(k, kind="getitem")
4729 try:
-> 4730 return self._engine.get_value(s, k, tz=getattr(series.dtype, "tz", None))
4731 except KeyError as e1:
4732 if len(self) > 0 and (self.holds_integer() or self.is_boolean()):
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_value()
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_value()
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()
pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()
KeyError: 2
是否与我的数据框索引不遵循1,2,3,4,5,6,7,8,9,10有关?有办法解决吗?这是由于之前对数据框进行了大量切片而导致的。已经非常感谢!
答案 0 :(得分:3)
您所需要的是其中之一吗?
df.groupby('CM')['zahlgesnet'].transform('sum')
输出
1 2.234,42234
3 0
4 2.234,42234
6 8.234134
7 653,23
9 8.234134
df.groupby('CM')['zahlgesnet'].sum()
输出
CM
0 2.234,42234
1 653,23
2 8.234134
3 0
答案 1 :(得分:1)
如果我了解您要执行的操作,则可以简单地使用groupby:
X_valid.groupby(['CM'])['zahlgesnet'].sum()
关于您的代码,请尝试:
SummeFF = 0
SummeFT = 0
SummeTF = 0
SummeTT = 0
result = 0
def getsums(x,y,z,v):
for i in range(len(X_valid)):
if X_valid.iloc[i].CM == 0:
x += (X_valid.iloc[i].zahlgesnet)
elif X_valid.iloc[i].CM == 1:
y += (X_valid.iloc[i].zahlgesnet)
elif X_valid.iloc[i].CM == 2:
z += (X_valid.iloc[i].zahlgesnet)
elif X_valid.iloc[i].CM == 3:
v += (X_valid.iloc[i].zahlgesnet)
getsums(SummeFF,SummeFT,SummeTF,SummeTT)
print(SummeFF,SummeFT,SummeTF,SummeTT)