Python: Corresponding CSV Row Values

98 Views Asked by At

For each corresponding value of the two columns (IPs and Platforms) specified in the code I want to concatenate them and then count the amount of the times this concatenated string occurs throughout the csv database.

This code currently requires almost an hour to go through 1000 rows of data in the command line, which seems to be a result of the double-for loop. If nothing else, how can I append the platform for each corresponding IP to the users_country_platform list without using this double-for loop?

import sys
import csv
from collections import Counter

users_country_platform = []
with open(sys.argv[1], 'r') as UserLog:
    IPs = [row['client_ip_address'] for row in csv.DictReader(UserLog)]
    UserLog.seek(0)
    platforms = [row['platform'] for row in csv.DictReader(UserLog)]
    for platform in platforms:
        for IP in IPs:
            if IP != 'NULL' and platform != 'NULL' or 'None':
               users_country_platform.append(str(response.country.iso_code) + ', ' + platform)

An alternative solution would be to establish a subarray within the country key so that the output would be in the form [country: platform: count], but then I think I would need a different counting method. Advice on either method would be appreciated.

1

There are 1 best solutions below

2
On BEST ANSWER

enter image description here

I used a very simple example but hopefully you can use the below and work it into your code. Taking the csv format above, consider the following:

import csv
import os
cwd = os.getcwd()

counter = {}
with open(cwd+'\\test.csv', 'rb') as file_in:
    reader = csv.DictReader(file_in, restval=None, restkey=None, dialect='excel')
    for row in reader:
        conc = row['ip'] + row['platform']
        counter.setdefault(conc, 0)
        counter[conc] += 1

print counter

Hopefully this shows how to count unique occurences of two columns with only one loop!