【问题标题】:Getting "TypeError: only integer scalar arrays can be converted to a scalar index" when working with Excel file使用 Excel 文件时出现“TypeError:只能将整数标量数组转换为标量索引”
【发布时间】:2020-11-06 17:24:26
【问题描述】:

我有一个 Excel 文件,其中的一列 (x) 行用字典填充。在此列上执行的任何操作都会给我这个错误:df.x,df.x.describe()。无法处理。

我已经通过手动创建相同的 df 进行检查,以检查它是否不是类型问题,但不是。

b=[{'maker':'renault','model':'twingo','year':'null','motor':'null','fitment':'null'}]
c=[5500]
df= pd.DataFrame({'country':a, 'x':b ,'sold':c}) 

你遇到过类似的问题吗?

这是完整的错误信息:

<ipython-input-11-04b9e65f2265> in <module>
      1 maker=[]
----> 2 for idx, row in test['x'].iteritems():
      3     l=row.keys()
      4 
      5     if 'maker' in l:

~\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

【问题讨论】:

    标签: python pandas dataframe dictionary


    【解决方案1】:

    我找到了。重命名列时出错。
    谢谢

    【讨论】:

      猜你喜欢
      • 2019-01-09
      • 2020-04-30
      • 2021-01-17
      • 2020-07-23
      • 2019-03-15
      • 2020-04-20
      • 2020-03-12
      • 2021-02-09
      • 1970-01-01
      相关资源
      最近更新 更多