简体   繁体   English

AttributeError: 'collections.OrderedDict' object 没有属性 'value_counts'

[英]AttributeError: 'collections.OrderedDict' object has no attribute 'value_counts'

I'm receiving an error message and can't seem to figure it out.我收到一条错误消息,似乎无法弄清楚。 It happens when I try to count the number of instances the value 0 occurs in ws1 column F starting at row 4, and then write it out to ws3 column M row 2. Has anyone encountered this error before?当我尝试从第 4 行开始计算 ws1 列 F 中出现值 0 的实例数,然后将其写入 ws3 列 M 第 2 行时,就会发生这种情况。以前有人遇到过这个错误吗?

Error
    counts = series.value_counts()

AttributeError: 'collections.OrderedDict' object has no attribute 'value_counts'

import openpyxl as xl 
import os
import pandas as pd 
  
input_dir = 'C:\\work\\comparison\\NNM'
template = 'C:\\work\\comparison\\template.xlsx'
summary = 'C:\\work\\comparison\\summary.xlsx'
newFile = 'Comparison.xlsx'
  
  
  
  
files = [file for file in os.listdir(input_dir)
         if os.path.isfile(file) and file.endswith(".xlsx")]
  
  
wb3 = xl.load_workbook(template) 
ws3 = wb3.worksheets[0] 
  
i=0
ii=0
  
  
for file in files: 
   input_file =  os.path.join(input_dir, file)
   wb1=xl.load_workbook(input_file)
   ws1=wb1.worksheets[0]
    
   series = pd.read_excel(input_file, sheetname=None, skiprows = [5], usecols = "F", squeeze = True) 
   counts = series.value_counts()
   z = counts[0]
     
   wb2 = xl.load_workbook(summary) 
   ws2 = wb2.worksheets[1]
  
      
   ws3[f'A{i+2}']=ws1['A1'].value[28:]
   ws3[f'D{i+2}']=ws1['B4'].value
   ws3[f'E{i+2}']=ws1['D4'].value
   ws3[f'I{i+2}']=ws1['B'][-1].value
   ws3[f'J{i+2}']=ws1['D'][-1].value
   ws3[f'O{i+2}']=ws1['E'][-1].value 
   ws3[f'N{i+2}']=ws2[f'I{ii+6}'].value  
   ws3[f'M{i+2}']=z 
   i += 1
   ii +=1          
     
     
  
   wb3.save(newFile)

When you use the sheet_name=None , you'll get a dict object not a Series or DataFrame object.当你使用sheet_name=None时,你会得到一个dict object 而不是SeriesDataFrame object。

And a dict object does not have the method .value_counts() . dict object 没有方法.value_counts()

In ur case, u could do like this:在你的情况下,你可以这样做:

 series = pd.read_excel(input_file, sheetname='ur_sheet_name', skiprows = [5], usecols = "F", squeeze = True) 

Or:或者:

series = pd.read_excel(input_file,skiprows = [5], squeeze = True)['title_of_column_F']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Django AttributeError: 'collections.OrderedDict' 对象没有属性 'pk' - Django AttributeError: 'collections.OrderedDict' object has no attribute 'pk' AttributeError: 'collections.OrderedDict' 对象没有属性 'split' - AttributeError: 'collections.OrderedDict' object has no attribute 'split' AttributeError: 'collections.OrderedDict' 对象没有属性 'iloc' - AttributeError: 'collections.OrderedDict' object has no attribute 'iloc' AttributeError: 'collections.OrderedDict' object 没有属性 'train' - AttributeError: 'collections.OrderedDict' object has no attribute 'train' Django - AttributeError: 'collections.OrderedDict' 对象没有属性 'id' - Django - AttributeError: 'collections.OrderedDict' object has no attribute 'id' “collections.OrderedDict”对象没有属性 - 'collections.OrderedDict' object has no attribute Django - 'collections.OrderedDict' 对象没有属性 'headers' - Django - 'collections.OrderedDict' object has no attribute 'headers' 'collections.OrderedDict' 对象没有属性 'pk' - django rest 框架 - 'collections.OrderedDict' object has no attribute 'pk' - django rest framework 预训练的 model 错误? 'collections.OrderedDict' object 没有属性 'eval' - pretrained model error ? 'collections.OrderedDict' object has no attribute 'eval' Value_counts() AttributeError: 'str' object 没有属性 'value_counts' - Value_counts() AttributeError: 'str' object has no attribute 'value_counts'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM