Python 3.x 由于类型错误,无法绘制散点图

Python 3.x 由于类型错误,无法绘制散点图,python-3.x,matplotlib,scatter-plot,numpy-ndarray,Python 3.x,Matplotlib,Scatter Plot,Numpy Ndarray,我有一个数据集,在其中我将只使用一个列来应用kmeans集群。然而,在绘制图表时,我得到了“numpy.ndarray”。我尝试转换为浮动,但仍然面临同样的问题 数据帧: Brim 1234.5 345 675.7 120 110 代码: 错误: --------------------------------------------------------------------------- TypeError

我有一个数据集,在其中我将只使用一个列来应用kmeans集群。然而,在绘制图表时,我得到了“numpy.ndarray”。我尝试转换为浮动,但仍然面临同样的问题

数据帧:

 Brim
 1234.5
 345
 675.7
 120
 110
代码:

错误:

   ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    <ipython-input-62-5f0966ccc828> in <module>()
     1 import matplotlib.pyplot as plt
     2 get_ipython().run_line_magic('matplotlib', 'inline')
  ----> 3 plt.scatter(df1[x ==1,0], df1[x == 0,1], s=100, c='red')
     4 plt.scatter(df1[x ==1,0], df1[x == 1,1], s=100, c='black')
     5 plt.scatter(df1[x ==2,0], df1[x == 2,1], s=100, c='blue')

     ~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
     2137             return self._getitem_multilevel(key)
     2138         else:
   ->2139             return self._getitem_column(key)
     2140 
     2141     def _getitem_column(self, key):

    ~\AppData\Local\Continuum\anaconda3\lib\site- 
 packages\pandas\core\frame.py in _getitem_column(self, key)
     2144         # get column
     2145         if self.columns.is_unique:
  -> 2146             return self._get_item_cache(key)
     2147 
     2148         # duplicate columns & possible reduce dimensionality

   ~\AppData\Local\Continuum\anaconda3\lib\site- packages\pandas\core\generic.py in _get_item_cache(self, item)
     1838         """Return the cached item, item represents a label indexer."""
     1839         cache = self._item_cache
  -> 1840         res = cache.get(item)
     1841         if res is None:
     1842             values = self._data.get(item)

   TypeError: unhashable type: 'numpy.ndarray'
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在()
1将matplotlib.pyplot作为plt导入
2 get_ipython()。运行_line_magic('matplotlib','inline')
---->3 plt.散射(df1[x==1,0],df1[x==0,1],s=100,c='red')
4 plt.散射(df1[x==1,0],df1[x==1,1],s=100,c='black')
5 plt.散射(df1[x==2,0],df1[x==2,1],s=100,c='blue')
~\AppData\Local\Continuum\anaconda3\lib\site packages\pandas\core\frame.py in\uuuuuu getitem\uuuuuu(self,key)
2137返回自我。\u获取项目\u多级(键)
2138其他:
->2139返回self.\u getitem\u列(键)
2140
2141 def_getitem_列(自身,键):
~\AppData\Local\Continuum\anaconda3\lib\site-
_getitem_列中的packages\pandas\core\frame.py(self,key)
2144#获取列
2145如果self.columns.u是唯一的:
->2146返回自我。获取项目缓存(密钥)
2147
2148#重复列和可能的降维
~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\generic.py在\u get\u item\u缓存中(self,item)
1838“返回缓存项,项表示标签索引器。”“”
1839缓存=自身。\u项目\u缓存
->1840 res=cache.get(项)
1841如果res为无:
1842 values=self.\u data.get(项目)
TypeError:不可损坏的类型:“numpy.ndarray”

如果我正确理解了您的代码,您正在尝试根据
x
的值对数据帧进行切片以进行打印。
为此,您应该使用
df1.loc[x==1,0]
而不是
df1[x==1,0]
(对于所有其他切片,依此类推)。

如果我正确理解了您的代码,您将尝试根据
x
的值对数据帧进行切片以进行打印。
为此,您应该使用
df1.loc[x==1,0]
而不是
df1[x==1,0]
(对所有其他切片如此)。

仍然存在错误,TypeError:由于只有一个变量,无法使用这些索引器[0]对其进行标签索引,在这种情况下如何绘制单变量图?@anaghas that(new?)错误听起来像您的数据帧有一个“字符串”索引(而不是0,1,2,…),所以您不能使用
x=[0,0,0,…3,3,]
(类型为
int64
)作为这里的掩码。打印(df1.index.dtype)和打印(x.dtype)的返回值是多少?仍然是错误,TypeError:无法使用这些索引器[0]对其进行标签索引,因为只有一个变量,在这种情况下如何绘制一个单变量图?@anaghas(new?)错误听起来像是您的数据帧有一个“字符串”索引(而不是例如0,1,2,…),因此您不能使用
x=[0,0,0,…3,3,]
(类型为
int64
)作为此处的掩码。打印(df1.index.dtype)和打印(x.dtype)的返回是什么?
   ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    <ipython-input-62-5f0966ccc828> in <module>()
     1 import matplotlib.pyplot as plt
     2 get_ipython().run_line_magic('matplotlib', 'inline')
  ----> 3 plt.scatter(df1[x ==1,0], df1[x == 0,1], s=100, c='red')
     4 plt.scatter(df1[x ==1,0], df1[x == 1,1], s=100, c='black')
     5 plt.scatter(df1[x ==2,0], df1[x == 2,1], s=100, c='blue')

     ~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
     2137             return self._getitem_multilevel(key)
     2138         else:
   ->2139             return self._getitem_column(key)
     2140 
     2141     def _getitem_column(self, key):

    ~\AppData\Local\Continuum\anaconda3\lib\site- 
 packages\pandas\core\frame.py in _getitem_column(self, key)
     2144         # get column
     2145         if self.columns.is_unique:
  -> 2146             return self._get_item_cache(key)
     2147 
     2148         # duplicate columns & possible reduce dimensionality

   ~\AppData\Local\Continuum\anaconda3\lib\site- packages\pandas\core\generic.py in _get_item_cache(self, item)
     1838         """Return the cached item, item represents a label indexer."""
     1839         cache = self._item_cache
  -> 1840         res = cache.get(item)
     1841         if res is None:
     1842             values = self._data.get(item)

   TypeError: unhashable type: 'numpy.ndarray'