Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#Write a function called clean _ data. clean _ data takes one #parameter, a dictionary. The dictionary represents the #observed rainfall in inches on a

#Write a function called clean_data. clean_data takes one
#parameter, a dictionary. The dictionary represents the
#observed rainfall in inches on a particular calendar day
#at a particular location. However, the data has some
#errors.
#
#clean_data should delete any key-value pair where the value
#has any of the following issues:
#
# - the type is not an integer or a float. Even if the value
# is a string that could be converted to an integer (e.g.
# "5") it should be deleted.
# - the value is less than 0: it's impossible to have a
# negative rainfall number, so this must be bad data.
# - the value is greater than 100: the world record for
# rainfall in a day was 71.8 inches
#
#Return the dictionary when you're done making your changes.
#
#Remember, the keyword del deletes items from lists
#and dictionaries. For example, to remove the key "key!" from
#the dictionary my_dict, you would write: del my_dict["key!"]
#Or, if the key was the variable my_key, you would write:
#del my_dict[my_key]
#
#Hint: If you try to delete items from the dictionary while
#looping through the dictionary, you'll run into problems!
#We should never change the number if items in a list or
#dictionary while looping through those items. Think about
#what you could do to keep track of which keys should be
#deleted so you can delete them after the loop is done.
#
#Hint 2: To check if a variable is an integer, use
#type(the_variable)== int. To check if a variable is a float,
#use type(the_variable)== float.
#Write your function here!
def clean_data(adict):
badlist=[]
for value in adict.values():
if type(value)==int or type(value)==float:
if value<0 or value>100:
badlist.append(value)
else:
pass
else:
badlist.append(value)
for badname in badlist:
del adict[badname]
return adict
#Below are some lines of code that will test your function.
#You can change the value of the variable(s) to test your
#function with different inputs.
#
#If your function works correctly, this will originally
#print (although the order of the keys may vary):
#{"20190101": 5,"20190103": 7.5,"20190104": 0,"20190107": 1}
rainfall ={"20190101": 5,"20190102": "6","20190103": 7.5,
"20190104": 0,"20190105": -7,"20190106": 102,
"20190107": 1}
print(clean_data(rainfall))
My function is under the part of code where it says "#Write your function here" I keep getting a Key Error. what is wrong?

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

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Prepare a short profile of Henry words worth Longfellow?

Answered: 1 week ago

Question

What is RAM as far as telecommunication is concerned?

Answered: 1 week ago

Question

Question 1: What is reproductive system? Question 2: What is Semen?

Answered: 1 week ago

Question

Describe the sources of long term financing.

Answered: 1 week ago

Question

4. Label problematic uses of language and their remedies

Answered: 1 week ago