请检查以下错误... commonlist中存在一些值,因此,我不希望包含commonlist的df_head值…或可以说df_head = df_head - commonlist

commonlist =df_head[df_head['Name'].isin(common)]
df_head not in commonlist


错误来了

TypeError                                 Traceback (most recent call last)
<ipython-input-16-ff85aff2f182> in <module>
----> 1 df_head not in commonlist

~\Anaconda3\lib\site-packages\pandas\core\generic.py in __contains__(self, key)
   1520     def __contains__(self, key):
   1521         """True if the key is in the info axis"""
-> 1522         return key in self._info_axis
   1523
   1524     @property

~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in __contains__(self, key)
   2033     @Appender(_index_shared_docs['__contains__'] % _index_doc_kwargs)
   2034     def __contains__(self, key):
-> 2035         hash(key)
   2036         try:
   2037             return key in self._engine

~\Anaconda3\lib\site-packages\pandas\core\generic.py in __hash__(self)
   1490     def __hash__(self):
   1491         raise TypeError('{0!r} objects are mutable, thus they cannot be'
-> 1492                         ' hashed'.format(self.__class__.__name__))
   1493
   1494     def __iter__(self):

TypeError: 'DataFrame' objects are mutable, thus they cannot be hashed

最佳答案

做就是了 :

df_head[~df_head['Name'].isin(common)]


~将取反df_head['Name'].isin(common)的值。

为此,实际上并不需要commonlist,除非您出于其他原因要存储它们。

关于python - 为什么我给我“DataFrame”对象的代码是可变的?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54546707/

10-13 09:35