Python Forum
AttributeError: 'collections.OrderedDict' object has no attribute 'value_counts
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
AttributeError: 'collections.OrderedDict' object has no attribute 'value_counts
#1
Hello,

I'm not sure why I'm receiving the error message at the very bottom, its for line 30. I'm just trying to count how many times the value 0 occurs in ws1 column F starting at row 4, and then write it out to ws3 column M row 2.

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)
Error Message
Error:
counts = series.value_counts() AttributeError: 'collections.OrderedDict' object has no attribute 'value_counts
Reply
#2
Hello,
I am not very familiar with the modules you are using, so I can't give very specific advice. But value_counts() seems to be a method of pandas dataframe. While pd.read_excel seems to return an ordered dictionary. I hope you can work on finding the issue from here.
Reply
#3
pd.read_excel() return a DataFrame and not a series.
# series = pd.read_excel(input_file, sheetname=None, skiprows = [5], usecols = "F", squeeze = True) 
df = pd.read_excel(input_file, sheetname=None, skiprows = [5], usecols = "F", squeeze = True)
Now on DataFrame can access a series bye name df.Age.value_counts() or df['Age'].value_counts(),if eg a column has name Age.
Reply
#4
(Jul-16-2020, 07:43 PM)snippsat Wrote: pd.read_excel() return a DataFrame and not a series.
# series = pd.read_excel(input_file, sheetname=None, skiprows = [5], usecols = "F", squeeze = True) 
df = pd.read_excel(input_file, sheetname=None, skiprows = [5], usecols = "F", squeeze = True)
Now on DataFrame can access a series bye name df.Age.value_counts() or df['Age'].value_counts(),if eg a column has name Age.


If I do this, it doesn't work correctly for some reason I get an error with Keyword:Speed which is the column header under column F. I can't use z=counts now either.

   df = pd.read_excel(file, sheet_name=None, usecols = 'F', squeeze = True) 
   df['Speed'].value_counts()
   z = counts[0]
Reply
#5
That's odd, I get a dataframe object (not Ordered dict) with pd.read_excel. The only thing I can think of is the sheet_name argument in read_excel. If you select None it is going to get the data in the cells you specify in all the sheets in the workbook (perhaps this is why the output is Ordered Dict as mentioned by j.crater, not dataframe as intended). Try entering the sheet you are interested in, or ignore the argument if there is only one sheet in the workbook).

Otherwise, can you please print series and show what you get?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  getpass.getpass() results in AttributeError: module 'os' has no attribute 'O_NOCTTY' EarthAndMoon 4 765 Oct-03-2023, 02:00 PM
Last Post: deanhystad
  AttributeError: '_tkinter.tkapp' object has no attribute 'username' Konstantin23 4 1,672 Aug-04-2023, 12:41 PM
Last Post: Konstantin23
  Python: Regex is not good for re.search (AttributeError: 'NoneType' object has no att Melcu54 9 1,470 Jun-28-2023, 11:13 AM
Last Post: Melcu54
  Parallel processing - AttributeError: Can't get attribute 'sktimekmeans' Mohana1983 1 744 Jun-22-2023, 02:33 AM
Last Post: woooee
  Python: AttributeError: 'PageObject' object has no attribute 'extract_images' Melcu54 2 3,865 Jun-18-2023, 07:47 PM
Last Post: Melcu54
  Object attribute behavior different in 2 scripts db042190 1 730 Jun-14-2023, 12:37 PM
Last Post: deanhystad
  cx_oracle Error - AttributeError: 'function' object has no attribute 'cursor' birajdarmm 1 2,331 Apr-15-2023, 05:17 PM
Last Post: deanhystad
  Pandas AttributeError: 'DataFrame' object has no attribute 'concat' Sameer33 5 5,592 Feb-17-2023, 06:01 PM
Last Post: Sameer33
  WebDriver' object has no attribute 'find_element_by_css_selector rickadams 3 5,900 Sep-19-2022, 06:11 PM
Last Post: Larz60+
  'dict_items' object has no attribute 'sort' Calli 6 4,475 Jul-29-2022, 09:19 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020