首页 > 解决方案 > 使用数字计算列的平均值

问题描述

我正在从数据框中计算 column1 的平均值:

df[1].mean

或者

df[:, 1].mean

它显示错误:

'in get_loc
raise KeyError(key) from err
KeyError: 1'

有人知道我该如何解决吗?(平均值应避免考虑标题行)

标签: pythonpandascsv

解决方案


如果列名确实是数字格式,您的代码应该可以工作(除了缺少()for )。mean例如:

df = pd.DataFrame({1: [1.0, 2.0, 3.0]})

df[1].mean()


Output:

2.0

这种错误的一种可能性是您的列名实际上是一个字符(1 个字母的字符串)。例如:

df = pd.DataFrame({'1': [1.0, 2.0, 3.0]})

df[1].mean()

Output:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   3079             try:
-> 3080                 return self._engine.get_loc(casted_key)
   3081             except KeyError as err:

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 1

The above exception was the direct cause of the following exception:

KeyError                                  Traceback (most recent call last)
<ipython-input-58-0fbd35b7b19f> in <module>
----> 1 df[1].mean()

~\anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
   3022             if self.columns.nlevels > 1:
   3023                 return self._getitem_multilevel(key)
-> 3024             indexer = self.columns.get_loc(key)
   3025             if is_integer(indexer):
   3026                 indexer = [indexer]

~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   3080                 return self._engine.get_loc(casted_key)
   3081             except KeyError as err:
-> 3082                 raise KeyError(key) from err
   3083 
   3084         if tolerance is not None:

KeyError: 1

在这种情况下,您应该将 括1在一对括号中:

df['1'].mean()

Output:

2.0

推荐阅读