Home > Mobile >  Compare 2 columns in one csv file(percentages)
Compare 2 columns in one csv file(percentages)

Time:08-09

I am a novice doing a course in python. My task is the following. I have a csv file (cameraData) containing several columns that looks like this:

IDnumber;Speedlimit;Speed;Date;Time
14075010;40;55;2021-09-11;11:15:31
14075010;40;54;2021-09-11;08:09:17
14075010;40;53;2021-09-11;13:02:41
14075010;40;49;2021-09-11;13:02:55

I want to compare the Speedlimit column with the Speed column. The idea is that the user can input a percentage and the program returns a list with the speeds that are x % over the speed limit. I am not allowed to use pandas. I have tried using dictreader and nested for loops to iterate through the 2 columns. I cant really figure out how to do it without pandas. I have also though about using zip() but I think it might be a bit of a workaround. I am unsure what is the best way to compare the columns(percentage wise). Here is my code so far:

import csv

#convert input to percentage decimal
percentoverspeedlimit = input("Input percentage over speed limit: ") 
percentage = float(percentoverspeedlimit) 
percentage_decimal = 1   (variable / 100)

#create new function and empty dict
def speedlimitplus(camera_data):
    overspeedlimit={}
    
#open file
    with open(camera_data, 'r') as p:
            csv_reader = csv.DictReader (p, delimiter = ';')
            for row in csv_reader:
                overspeedlimit[row['Speedlimit']] = overspeedlimit.get(row['Speedlimit'], 0)   1
                
    return overspeedlimit

#use function
camera_data = speedlimitplus('cameraData.csv')

print(overspeedlimit)

I have been struggling to find example code that isn´t pandas. Resources, example code or hints/tips are appreciated. Thanks.

CodePudding user response:

I'm not entirely sure what you're trying to achieve with this line:

overspeedlimit[row['Speedlimit']] = overspeedlimit.get(row['Speedlimit'], 0)   1

To solve this problem, you can indeed use csv.DictReader to parse the data into an iterable of dictionaries containing the data for each row. In your loop instead of the above you can directly access the Speedlimit and the Speed, compare them against the user inputted percentage and print when neccessary:

import csv

if __name__ == '__main__':
    percentage = float(input(r'% over speed limit to filter by: '))
    with open('./cameraData.csv', 'r') as csvfile:
        reader = csv.DictReader(csvfile, delimiter=';')
        for row in reader:
            if float(row['Speed']) / float(row['Speedlimit']) * 100 > percentage:
                # do something to row dictionary here before printing?
                print(row)

Which gives:

% over speed limit to filter by: 10
{'IDnumber': '14075010', 'Speedlimit': '40', 'Speed': '55', 'Date': '2021-09-11', 'Time': '11:15:31'}
{'IDnumber': '14075010', 'Speedlimit': '40', 'Speed': '54', 'Date': '2021-09-11', 'Time': '08:09:17'}
{'IDnumber': '14075010', 'Speedlimit': '40', 'Speed': '53', 'Date': '2021-09-11', 'Time': '13:02:41'}
{'IDnumber': '14075010', 'Speedlimit': '40', 'Speed': '49', 'Date': '2021-09-11', 'Time': '13:02:55'}

CodePudding user response:

You can define a fucntion that will get user input percentage and list that contains tuples of "Speed Limit" and "Speed", compare with input and print;

def compare(input_percentage, data_list):
    for i in data_list:
        percentage = (i[1]-i[0]/i[1])*100
        if percentage > input_percentage:
            print(f"Over Speed Percentage is higher than input percentage: {i}")
        else:
            pass

You can construct this function to your need. You can also generate dictionary when importing csv and use that too. To create dictionary reading csv file check this post : Creating a dictionary from a csv file?

If you pass dictionary to function you can also create new key and value regarding your comparison.

Function that gets user input and dictionary:

dictionary_list = [{"IDnumber: 14075010,"
                    "Speedlimit": 40,
                    "Speed": 55,
                    "Date": "2021-09-11",
                    "Time": "11:15:31"
                   ]

def compare(input_percentage, dictionary_list):
    for item  in dictionary_list:
        percentage = (item["Speedlimit"] - item["Speed"]) / item["Speed"] * 100
        if percentage > input_percentage:
            print(f"ID number: {item['IDnumber']} is over input percentage")
        else:
            pass
  • Related