无法从数据帧(Python)获取序列

无法从数据帧(Python)获取序列,python,pandas,dataframe,series,Python,Pandas,Dataframe,Series,大家好。我一直在使用通过Anaconda Navigator打开的JupyterLab,并编写了上述代码。前三行看起来正常,但是,对于第四行,我得到了一个错误,如下所示。如果我把第四行改成dg[[“No1”]],那么它“起作用了”。然而,在这种情况下,类型(dg[[“No1”]])实际上是数据帧,而不是序列 我有点糊涂,我已经搔了将近两个小时的头,仍然不明白出了什么问题。有人能帮忙吗?谢谢 TypeError回溯(最近一次调用) 在里面 ---->1 dg[“1号”] ~\anaconda3\

大家好。我一直在使用通过Anaconda Navigator打开的JupyterLab,并编写了上述代码。前三行看起来正常,但是,对于第四行,我得到了一个错误,如下所示。如果我把第四行改成dg[[“No1”]],那么它“起作用了”。然而,在这种情况下,类型(dg[[“No1”]])实际上是数据帧,而不是序列

我有点糊涂,我已经搔了将近两个小时的头,仍然不明白出了什么问题。有人能帮忙吗?谢谢


TypeError回溯(最近一次调用)
在里面
---->1 dg[“1号”]
~\anaconda3\lib\site packages\pandas\core\frame.py in\uuuu\getitem\uuuuuuu(self,key)
2774如果self.columns.nlevels>1:
2775返回自我。\u获取项目\u多级(键)
->2776返回自我。\u获取\u项目\u缓存(密钥)
2777
我们有切片机(成行)吗?
~\anaconda3\lib\site packages\pandas\core\generic.py在\u get\u item\u缓存中(self,item)
3584 res=cache.get(项)
3585如果res为无:
->3586 values=self.\u data.get(项目)
3587 res=自身。_框_项_值(项,值)
3588缓存[项目]=res
get中的~\anaconda3\lib\site packages\pandas\core\internals\managers.py(self,item)
966 raise VALUERROR(“无法使用空键标记索引”)
967
-->968返回自我iget(loc)
969其他:
970
iget中的~\anaconda3\lib\site packages\pandas\core\internals\managers.py(self,i)
983否则返回作为一个数组
984         """
-->985块=自块[self._blknos[i]]
986值=block.iget(self.\u blklocs[i])
987
TypeError:只能将整数标量数组转换为标量索引

您可以这样做,除非您想要多索引:

TypeError                                 Traceback (most recent call last)
<ipython-input-393-b26f43cf53bf> in <module>
----> 1 dg["No1"]

~\anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
   2774                 if self.columns.nlevels > 1:
   2775                     return self._getitem_multilevel(key)
-> 2776                 return self._get_item_cache(key)
   2777 
   2778         # Do we have a slicer (on rows)?

~\anaconda3\lib\site-packages\pandas\core\generic.py in _get_item_cache(self, item)
   3584         res = cache.get(item)
   3585         if res is None:
-> 3586             values = self._data.get(item)
   3587             res = self._box_item_values(item, values)
   3588             cache[item] = res

~\anaconda3\lib\site-packages\pandas\core\internals\managers.py in get(self, item)
    966                         raise ValueError("cannot label index with a null key")
    967 
--> 968             return self.iget(loc)
    969         else:
    970 

~\anaconda3\lib\site-packages\pandas\core\internals\managers.py in iget(self, i)
    983         Otherwise return as a ndarray
    984         """
--> 985         block = self.blocks[self._blknos[i]]
    986         values = block.iget(self._blklocs[i])
    987 

TypeError: only integer scalar arrays can be converted to a scalar index

您可以这样做,除非您想要多索引:

TypeError                                 Traceback (most recent call last)
<ipython-input-393-b26f43cf53bf> in <module>
----> 1 dg["No1"]

~\anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
   2774                 if self.columns.nlevels > 1:
   2775                     return self._getitem_multilevel(key)
-> 2776                 return self._get_item_cache(key)
   2777 
   2778         # Do we have a slicer (on rows)?

~\anaconda3\lib\site-packages\pandas\core\generic.py in _get_item_cache(self, item)
   3584         res = cache.get(item)
   3585         if res is None:
-> 3586             values = self._data.get(item)
   3587             res = self._box_item_values(item, values)
   3588             cache[item] = res

~\anaconda3\lib\site-packages\pandas\core\internals\managers.py in get(self, item)
    966                         raise ValueError("cannot label index with a null key")
    967 
--> 968             return self.iget(loc)
    969         else:
    970 

~\anaconda3\lib\site-packages\pandas\core\internals\managers.py in iget(self, i)
    983         Otherwise return as a ndarray
    984         """
--> 985         block = self.blocks[self._blknos[i]]
    986         values = block.iget(self._blklocs[i])
    987 

TypeError: only integer scalar arrays can be converted to a scalar index

太没办法理解什么是多索引,但它解决了我的问题…也许我所遵循的教科书有点过时…谢谢!太没办法理解什么是多索引,但它解决了我的问题…也许我所遵循的教科书有点过时…谢谢!
dg.columns = ["No1", "No2", "No3", "No4"]