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

问题描述 投票:0回答:1
a = np.random.standard_normal((9,4))
dg = pd.DataFrame(a)
dg.columns = [["No1", "No2", "No3", "No4"]]
dg["No1"]

大家好。我一直在使用通过Anaconda Navigator打开的JupyterLab,并编写了上面的代码。前三行看起来很正常,但是对于第四行,我得到了如下错误。如果我将第四行更改为dg [[“ No1”]],则它“有效”。但是,在这种情况下,type(dg [[“ No1”]])实际上是数据帧,而不是序列。

我有点笨,我已经挠头了近两个小时,但仍然不明白这是怎么回事。有人可以帮忙吗?谢谢!!


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
    

a = np.random.standard_normal((9,4))dg = pd.DataFrame(a)dg.columns = [[“” No1“,” No2“,” No3“,” No4“]] dg [” No1“]大家好。我一直在使用通过Anaconda Navigator打开的JupyterLab,并且写了...

python pandas dataframe series
1个回答
0
投票

您可以执行此操作,除非您需要多索引:

© www.soinside.com 2019 - 2024. All rights reserved.