Pandas 从dataframe列中查找max元素时出错
我试图从数据帧中的列中查找最大的元素,但这会导致以下错误。 我已经测试过,它只会给这个列名带来错误,其余的列工作正常 这是从posts1.csv文件创建的数据帧Pandas 从dataframe列中查找max元素时出错,pandas,dataframe,machine-learning,max,slice,Pandas,Dataframe,Machine Learning,Max,Slice,我试图从数据帧中的列中查找最大的元素,但这会导致以下错误。 我已经测试过,它只会给这个列名带来错误,其余的列工作正常 这是从posts1.csv文件创建的数据帧 import pandas as pd posts_n = pd.read_csv('posts1.csv',encoding='latin-1') posts=posts_n.fillna(0) 当我试图从特定列(即“分数”)中查找max元素时 我得到以下错误 KeyError
import pandas as pd
posts_n = pd.read_csv('posts1.csv',encoding='latin-1')
posts=posts_n.fillna(0)
当我试图从特定列(即“分数”)中查找max元素时
我得到以下错误
KeyError Traceback (most recent call last)
~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
2441 try:
-> 2442 return self._engine.get_loc(key)
2443 except KeyError:
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: 'score'
During handling of the above exception, another exception occurred:
KeyError Traceback (most recent call last)
<ipython-input-12-09c353ba0de2> in <module>()
34 #MAximum posts done by a user
35
---> 36 max_post = posts['score'].max()
37 max_post
38 #scr=posts.iloc[:,4]
~\Anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
1962 return self._getitem_multilevel(key)
1963 else:
-> 1964 return self._getitem_column(key)
1965
1966 def _getitem_column(self, key):
~\Anaconda3\lib\site-packages\pandas\core\frame.py in _getitem_column(self, key)
1969 # get column
1970 if self.columns.is_unique:
-> 1971 return self._get_item_cache(key)
1972
1973 # duplicate columns & possible reduce dimensionality
~\Anaconda3\lib\site-packages\pandas\core\generic.py in _get_item_cache(self, item)
1643 res = cache.get(item)
1644 if res is None:
-> 1645 values = self._data.get(item)
1646 res = self._box_item_values(item, values)
1647 cache[item] = res
~\Anaconda3\lib\site-packages\pandas\core\internals.py in get(self, item, fastpath)
3588
3589 if not isnull(item):
-> 3590 loc = self.items.get_loc(item)
3591 else:
3592 indexer = np.arange(len(self.items))[isnull(self.items)]
~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
2442 return self._engine.get_loc(key)
2443 except KeyError:
-> 2444 return self._engine.get_loc(self._maybe_cast_indexer(key))
2445
2446 indexer = self.get_indexer([key], method=method, tolerance=tolerance)
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: 'score'
keyrerror回溯(最近一次调用)
get\u loc中的~\Anaconda3\lib\site packages\pandas\core\index\base.py(self、key、method、tolerance)
2441请尝试:
->2442自动返回发动机。获取锁定位置(钥匙)
2443键错误除外:
熊猫\\u libs\index.pyx在熊猫中。\ u libs.index.IndexEngine.get_loc()
熊猫\\u libs\index.pyx在熊猫中。\ u libs.index.IndexEngine.get_loc()
pandas\\u libs\hashtable\u class\u helper.pxi在pandas.\u libs.hashtable.PyObjectHashTable.get\u item()中
pandas\\u libs\hashtable\u class\u helper.pxi在pandas.\u libs.hashtable.PyObjectHashTable.get\u item()中
关键错误:“分数”
在处理上述异常期间,发生了另一个异常:
KeyError回溯(最近一次呼叫最后一次)
在()
34#用户完成的最大帖子数
35
--->36 max_post=posts['score'].max()
37最高职位
38#scr=posts.iloc[:,4]
~\Anaconda3\lib\site packages\pandas\core\frame.py in\uuuu\getitem\uuuuuuu(self,key)
1962返回自我。\u获取项目\u多级(键)
1963年其他:
->1964返回自我。\u获取项目\u列(键)
1965
1966 def_getitem_列(自身,键):
~\Anaconda3\lib\site packages\pandas\core\frame.py在\u getitem\u列中(self,key)
1969年#获得专栏
1970如果self.columns.u是唯一的:
->1971返回自我。获取项目缓存(密钥)
1972
1973#重复列和可能的降维
~\Anaconda3\lib\site packages\pandas\core\generic.py在\u get\u item\u缓存中(self,item)
1643 res=cache.get(项)
1644如果res为无:
->1645 values=self.\u data.get(项目)
1646 res=自身。_框_项_值(项,值)
1647缓存[项目]=res
get中的~\Anaconda3\lib\site packages\pandas\core\internals.py(self、item、fastpath)
3588
3589如果不为空(项目):
->3590 loc=自身物品。获取loc(物品)
3591其他:
3592 indexer=np.arange(len(self.items))[isnull(self.items)]
get\u loc中的~\Anaconda3\lib\site packages\pandas\core\index\base.py(self、key、method、tolerance)
2442自动返回发动机。获取锁定位置(钥匙)
2443键错误除外:
->2444返回self.\u引擎。获取位置(self.\u可能\u投射\u索引器(键))
2445
2446 indexer=self.get_indexer([key],method=method,tolerance=tolerance)
熊猫\\u libs\index.pyx在熊猫中。\ u libs.index.IndexEngine.get_loc()
熊猫\\u libs\index.pyx在熊猫中。\ u libs.index.IndexEngine.get_loc()
pandas\\u libs\hashtable\u class\u helper.pxi在pandas.\u libs.hashtable.PyObjectHashTable.get\u item()中
pandas\\u libs\hashtable\u class\u helper.pxi在pandas.\u libs.hashtable.PyObjectHashTable.get\u item()中
关键错误:“分数”
这就是数据的外观
'score'
不在(列)索引中,因此您可以将其作为数据读入,而不是作为标题行载入csv的第一行
请尝试以下操作:
posts=pd.read\u csv('posts1.csv',header=1)
我写了这个posts\n=pd.read\u csv('posts1.csv',encoding='latin-1',header=1)还是一样的错误请将posts.head()添加到你的post@tobsecret我的意思是在posts=posts\u n.fillna(0)
之后,请添加一个print(posts.head())
并相应地编辑您的帖子,所以我知道你的数据框里到底有什么。我找到了解决方案,这很有效。posts\u n=pd.read\u csv('posts1.csv',encoding='latin-1',sep='\s*,\s*'))
KeyError Traceback (most recent call last)
~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
2441 try:
-> 2442 return self._engine.get_loc(key)
2443 except KeyError:
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: 'score'
During handling of the above exception, another exception occurred:
KeyError Traceback (most recent call last)
<ipython-input-12-09c353ba0de2> in <module>()
34 #MAximum posts done by a user
35
---> 36 max_post = posts['score'].max()
37 max_post
38 #scr=posts.iloc[:,4]
~\Anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
1962 return self._getitem_multilevel(key)
1963 else:
-> 1964 return self._getitem_column(key)
1965
1966 def _getitem_column(self, key):
~\Anaconda3\lib\site-packages\pandas\core\frame.py in _getitem_column(self, key)
1969 # get column
1970 if self.columns.is_unique:
-> 1971 return self._get_item_cache(key)
1972
1973 # duplicate columns & possible reduce dimensionality
~\Anaconda3\lib\site-packages\pandas\core\generic.py in _get_item_cache(self, item)
1643 res = cache.get(item)
1644 if res is None:
-> 1645 values = self._data.get(item)
1646 res = self._box_item_values(item, values)
1647 cache[item] = res
~\Anaconda3\lib\site-packages\pandas\core\internals.py in get(self, item, fastpath)
3588
3589 if not isnull(item):
-> 3590 loc = self.items.get_loc(item)
3591 else:
3592 indexer = np.arange(len(self.items))[isnull(self.items)]
~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
2442 return self._engine.get_loc(key)
2443 except KeyError:
-> 2444 return self._engine.get_loc(self._maybe_cast_indexer(key))
2445
2446 indexer = self.get_indexer([key], method=method, tolerance=tolerance)
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: 'score'