Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8997303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:52:17+00:00 2026-06-15T23:52:17+00:00

I am making a Python script that parses an Excel file using the xlrd

  • 0

I am making a Python script that parses an Excel file using the xlrd library.
What I would like is to do calculations on different columns if the cells contain a certain value. Otherwise, skip those values. Then store the output in a dictionary.
Here’s what I tried to do :

import xlrd


workbook = xlrd.open_workbook('filter_data.xlsx')
worksheet = workbook.sheet_by_name('filter_data')

num_rows = worksheet.nrows -1
num_cells = worksheet.ncols - 1

first_col = 0
scnd_col = 1
third_col = 2

# Read Data into double level dictionary
celldict = dict()
for curr_row in range(num_rows)  :

    cell0_val = int(worksheet.cell_value(curr_row+1,first_col))
    cell1_val = worksheet.cell_value(curr_row,scnd_col)
    cell2_val = worksheet.cell_value(curr_row,third_col)

    if cell1_val[:3] == 'BL1' :
        if cell2_val=='toSkip' :
        continue
    elif cell1_val[:3] == 'OUT' :
        if cell2_val == 'toSkip' :
        continue
    if not cell0_val in celldict :
        celldict[cell0_val] = dict()
# if the entry isn't in the second level dictionary then add it, with count 1
    if not cell1_val in celldict[cell0_val] :
        celldict[cell0_val][cell1_val] = 1
        # Otherwise increase the count
    else :
        celldict[cell0_val][cell1_val] += 1

So here as you can see, I count the number of “cell1_val” values for each “cell0_val”. But I would like to skip those values which have “toSkip” in the adjacent column’s cell before doing the sum and storing it in the dict.
I am doing something wrong here, and I feel like the solution is much more simple.
Any help would be appreciated. Thanks.

Here’s an example of my sheet :

cell0 cell1  cell2
12    BL1    toSkip
12    BL1    doNotSkip
12    OUT3   doNotSkip
12    OUT3   toSkip
13    BL1    doNotSkip
13    BL1    toSkip
13    OUT3   doNotSkip
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T23:52:18+00:00Added an answer on June 15, 2026 at 11:52 pm

    Use collections.defaultdict with collections.Counter for your nested dictionary.

    Here it is in action:

    >>> from collections import defaultdict, Counter
    >>> d = defaultdict(Counter)
    >>> d['red']['blue'] += 1
    >>> d['green']['brown'] += 1
    >>> d['red']['blue'] += 1
    >>> pprint.pprint(d)
    {'green': Counter({'brown': 1}),
     'red': Counter({'blue': 2})}
    

    Here it is integrated into your code:

    from collections import defaultdict, Counter
    import xlrd
    
    workbook = xlrd.open_workbook('filter_data.xlsx')
    worksheet = workbook.sheet_by_name('filter_data')
    
    first_col = 0
    scnd_col = 1
    third_col = 2
    
    celldict = defaultdict(Counter)
    for curr_row in range(1, worksheet.nrows): # start at 1 skips header row
    
        cell0_val = int(worksheet.cell_value(curr_row, first_col))
        cell1_val = worksheet.cell_value(curr_row, scnd_col)
        cell2_val = worksheet.cell_value(curr_row, third_col)
    
        if cell2_val == 'toSkip' and cell1_val[:3] in ('BL1', 'OUT'):
            continue
    
        celldict[cell0_val][cell1_val] += 1
    

    I also combined your if-statments and changed the calculation of curr_row to be simpler.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a python script that should zip a file and upload it at
I'm currently making a filesystem using python-fuse and was looking up where file pointers
I am making a Python script which will read in a GeoTIFF file, and
I have a python script that makes a series of url calls using urllib2.
In Alex Martelli's response to Making a Python script Object-Oriented , he mentions that
I have a python script that can output a plot using matplotlib and command
I'm writing a python script that takes a file one at a time or
I have to code a really annoying script that uses one excel file to
I'm making a python script that should run in the background and notify a
I'm making a python script that needs to do 3 things simultaneously. What is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.