Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Question 2 : generate _ descriptors ( ) Write a function called generate _ descriptors ( df ) which takes in the DataFrame you loaded

Question 2: generate_descriptors()
Write a function called generate_descriptors(df) which takes in the DataFrame you loaded from Question1 and returns a dataframe of all unique ticket descriptions and how frequent they are (e.g. it will tell you how many "HANDICAP" or "NO PERMITS U/M" tickets have been issued) for each of the following three time periods: morning (3 am to 11:59 am), afternoon (12 pm -5:59 pm), and evening (6 pm -2:59 am).
Make sure you drop na values of input df.
The DataFrame which generate_descriptors(df) returns should have 3 rows and 51 columns.
Index should be labelled as Morning, Afternoon, and Evening.
Column names should be unique values of df["Description"].
Need help with question 2
here is my code:
def generate_descriptors(df):
import xlrd
import pandas as pd
import numpy as np
import re
# Filter all warnings. If you would like to see the warnings, please comment the two lines below.
import warnings
warnings.filterwarnings('ignore')
# YOUR CODE HERE
# Preprocess the data by dropping NA values
df = df.dropna()
df['Time']= pd.to_datetime(df['Time'])
df['Period']= pd.cut(df['Time'].dt.hour, bins=[0,3,12,18,24], labels=['Evening', 'Morning', 'Afternoon', 'Evening'], include_lowest=True)
df_grouped = df.groupby(['Period', 'Description']).size().unstack().fillna(0)
df_grouped = df_grouped.loc[['Morning', 'Afternoon', 'Evening']]
return df
im getting an error.
Important to refer to question 1:
Question 1: load_ticket_data()
First, write the code to create a single DataFrame object in a function called load_ticket_data(). This function should return the full dataframe and take no parameters (you can assume the ticket files are in the same directory as your assignment notebook). Column labels should be as follows:
['Ticket #', 'Badge', 'Issue Date ', 'IssueTime', 'Plate', 'State', 'Make', 'Model', 'Violation', ' Description', 'Location', 'Meter', ' Fine ', 'Penalty']
Here are some hints.
Be sure to scroll through every single sheet to make sure what rows should be dropped.
Make sure to exclude unnecessary footers and headers from the datafile.
Check if your header labels are correct.
here is the answer:
def load_ticket_data():
import xlrd
import pandas as pd
import numpy as np
import re
# Filter all warnings. If you would like to see the warnings, please comment the two lines below.
import warnings
warnings.filterwarnings('ignore')
# YOUR CODE HERE
# List of column labels
column_labels =[
"Ticket #",
"Badge",
"Issue Date ",
"IssueTime",
"Plate",
"State",
"Make",
"Model",
"Violation",
"Description",
"Location",
"Meter",
"Fine",
"Penalty",
]
# Prepare a list
# reading all excel files
excel_files =[
{
"name": "AnnArbor-TicketViolation2015.xls", # EXCEL FILE 01
"sheets": [
{"name": "Sheet1", "skip_rows": 2, "skip_footer": 0}, # SHEET 1 INFO
{"name": "Sheet2", "skip_rows": 1, "skip_footer": 0}, # SHEET 2 INFO
{"name": "Sheet3", "skip_rows": 1, "skip_footer": 1}, # SHEET 3 INFO
],
},
{
"name": "AnnArbor-TicketViolation2016.xls", # EXCEL FILE 02
"sheets": [
{"name": "Sheet1", "skip_rows": 2, "skip_footer": 0}, # SHEET 1 INFO
{"name": "Sheet2", "skip_rows": 1, "skip_footer": 0}, # SHEET 2 INFO
{"name": "Sheet3", "skip_rows": 1, "skip_footer": 1}, # SHEET 3 INFO
],
},
{
"name": "AnnArbor-TicketViolation2017.xls", # EXCEL FILE 03
"sheets": [
{"name": "Sheet1", "skip_rows": 2, "skip_footer": 0}, # SHEET 1 INFO
{"name": "Sheet2", "skip_rows": 1, "skip_footer": 0}, # SHEET 2 INFO
{"name": "Sheet3", "skip_rows": 1, "skip_footer": 1}, # SHEET 3 INFO
],
},
{
"name": "AnnArbor-TicketViolation2018.xls", # EXCEL FILE 04
"sheets": [
{"name": "Sheet1", "skip_rows": 2, "skip_footer": 0}, # SHEET 1 INFO
{"name": "Sheet2", "skip_rows": 1, "skip_footer": 0}, # SHEET 2 INFO
{"name": "Sheet3", "skip_rows": 1, "skip_footer": 1}, # SHEET 3 INFO
],
},
{
"name": "AnnArbor-TicketViolation2019.xls", # EXCEL FILE 05
"sheets": [
{"name": "Sheet1", "skip_rows": 2, "skip_footer": 0}, # SHEET 1 INFO
{"name": "Sheet2", "skip_rows": 1, "skip_footer": 0}, # SHEET 2 INFO
{"name": "Sheet3", "skip_rows": 1, "skip_footer": 1}, # SHEET 3 INFO
],
},
{
"name": "AnnArbor-TicketViolation-jan2020.xls", # EXCEL FILE 06
"

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

7. Define cultural space.

Answered: 1 week ago

Question

8. Describe how cultural spaces are formed.

Answered: 1 week ago