Home > Software design >  Iterating for multiple conditions using itemgetter()/ Counter?
Iterating for multiple conditions using itemgetter()/ Counter?

Time:11-11

I am new to Python. I have a CSV file that I am parsing and am looking to return information that meets two conditions.

The data contains complaints about consumer financial products and services. 16 columns are within the file and I am looking to meet conditions for two of them, ['Product'] and ['Timely Response]. I am looking to get the percentage of timely responses to the most occurring ['Product']. ['Product'] contains 9 products and ['Timely Response'] is a Yes/No field.

I used itemgetter() to return the most complained about products:

for row in reader:
    id_counts = Counter(map(itemgetter(1), reader))
    pprint (id_counts)

returning:

Counter({'Credit reporting, credit repair services, or other personal consumer reports': 112,
         'Debt collection': 32,
         'Mortgage': 12,
         'Credit card or prepaid card': 11,
         'Checking or savings account': 11,
         'Student loan': 5,
         'Money transfer, virtual currency, or money service': 4,
         'Vehicle loan or lease': 4,
         'Payday loan, title loan, or personal loan': 1})

I am now looking to count the timely responses to the most common complaint.

for row in reader:
    if row[1] == 'Credit reporting, credit repair services, or other personal consumer reports':
        c = Counter(map(itemgetter(15), reader))
        print (c)
Counter({'Yes': 186, 'No': 4})

This is incorrect and is counting Yes/No from all fields.

I also tried:

for row in reader:
    if row[1] == 'Credit reporting, credit repair services, or other personal consumer reports':
        c = Counter(row[15].split())
        print (sum(c))

which returned an unsupported operand error.

I would like to solve this solution using getitem or Counter since that's what I began with, but any help/recommendations are greatly appreciated.

CodePudding user response:

    c = Counter(map(itemgetter(15), reader))

You are reading all (remaining) elements from reader, but you want to read only from the current row.

You can solve this with itemgetter;

c = Counter()
for row in reader:
    if row[1] == 'Credit reporting, credit repair services, or other personal consumer reports':
        c.update([itemgetter(15)(row)])
print (c)

... but this is enormously more complex than the obvious

c = Counter({"Yes": 0, "No": 0})
for row in reader:
    if row[1] == 'Credit reporting, credit repair services, or other personal consumer reports':
        c[row[15]]  = 1
print (c)

Perhaps the crucial observation here is that you are only looking at one row at a time. Your logic seems to assume you are examining all matching rows, but you are really only looping over them one by one and examining each in turn.

... If you wanted to be fancy, you could say

c = Counter(
  map(itemgetter(15),
    filter(lambda row: row[1] == 'Credit reporting, credit repair services, or other personal consumer reports',
      reader)))
  • Related