Question
fine_per_plates() Starting in 2004 Michigan moved to issuing plates with the format of ABC1234. That got me thinking, how many vanity plate holders there are
fine_per_plates()
Starting in 2004 Michigan moved to issuing plates with the format of ABC1234. That got me thinking, how many vanity plate holders there are in our dataset? Count the number of Michigan vehicles with plates in the following formats that have received a ticket:
- ABC1234
- ABC123
- 123ABC
- Vanity Plates (i.e. anything other than the aforementioned formats, including missing or NaN values)
Complete the function fine_per_plates() returning a dictionary. The dictinary should be formatted as follows:
plates_dict = {"ABC1234":the_number_of_vehicles, "ABC123":the_number_of_vehicles, "123ABC":the_number_of_vehicles, "vanity":the_number_of_vehicles}
This is my code: import pandas as pd
import re
def fine_per_plates():
# Load the ticket data from 'random_ticket_data'
df = pd.read_csv('random_ticket_data.csv')
# Define regular expressions for different plate formats
abc1234_regex = r'^[A-Z]{3}\d{4}$'
abc123_regex = r'^[A-Z]{3}\d{3}$'
m123abc_regex = r'^\d{3}[A-Z]{3}$'
# Create a new column 'plate_format' to store the plate format for each ticket
df['plate_format'] = df['Plate'].apply(lambda x: 'vanity' if not re.match(abc1234_regex, x) and not re.match(abc123_regex, x) and not re.match(m123abc_regex, x) else x)
# Initialize a dictionary to store the counts
plates_dict = {}
# Loop through each plate format
for plate_format in ['ABC1234', 'ABC123', '123ABC', 'vanity']:
# Define the corresponding regex pattern for the plate format
plate_regex = abc1234_regex if plate_format == 'ABC1234' else abc123_regex if plate_format == 'ABC123' else m123abc_regex if plate_format == '123ABC' else r'^((?!(ABC1234|ABC123|123ABC)).)*$'
# Count the number of vehicles with the specified plate format
plate_count = len(df[df['plate_format'].str.match(plate_regex)])
# Calculate the average fine for the specified plate format
plate_fine = df[df['plate_format'].str.match(plate_regex)]['Fine'].mean()
# Store the count and average fine in the plates_dict
plates_dict[plate_format] = {'the_number_of_vehicles': plate_count, 'the_average_fine': plate_fine}
# Return the dictionary with counts and average fines
return plates_dict
# Call the function and store the results in a variable
results = fine_per_plates()
# Print the results
print(results)
this is the output : FileNotFoundError Traceback (most recent call last) Cell In[57], line 37 34 return plates_dict 36 # Call the function and store the results in a variable ---> 37 results = fine_per_plates() 39 # Print the results 40 print(results) Cell In[57], line 6, in fine_per_plates() 4 def fine_per_plates(): 5 # Load the ticket data from 'random_ticket_data' ----> 6 df = pd.read_csv('random_ticket_data.csv') 8 # Define regular expressions for different plate formats 9 abc1234_regex = r'^[A-Z]{3}\d{4}$' File /opt/conda/lib/python3.9/site-packages/pandas/util/_decorators.py:211, in deprecate_kwarg.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
The error message indicates that the file randomticketda...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started