Compare your project to our solution code and share your project below! Your solution might not look exactly like ours, and that’s okay! The most important thing right now is to get your code working as it should (you can always refactor more later). There are multiple ways to complete these projects and you should exercise your creative abilities in doing so.
This is a safe space for you to ask questions about any sample solution code and share your work with others! Simply reply to this thread to get the conversation started. Feedback is a vital component in getting better with coding and all ability levels are welcome here, so don’t be shy!
About community guidelines: This is a supportive and kind community of people learning and developing their skills. All comments here are expected to keep to our community guidelines
How do I share my own solutions?
If you completed the project off-platform, you can upload your project to your own GitHub and share the public link on the relevant project topic.
If you completed the project in the Codecademy learning environment, use the share code link at the bottom of your code editor to create a gist, and then share that link here.
Do I really need to get set up on GitHub?
Yes! Both of these sharing methods require you to get set up on GitHub, and trust us, it’s worth your time. Here’s why:
Once you have your project in GitHub, you’ll be able to share proof of your work with potential employers, and link out to it on your CV.
It’s a great opportunity to get your feet wet using a development tool that tech workers use on the job, every day.
Not sure how to get started? We’ve got you covered - read this article for the easiest way to get set up on GitHub.
Best practices for asking questions about the sample solution
Be specific! Reference exact line numbers and syntax so others are able to identify the area of the code you have questions about.
Hi can anyone explain how the code works in the filter function:
def filter_data(data, words):
filter = lambda x: all(word.lower() in x.lower() for word in words)
return data.loc[data[“Question”].apply(filter)]
i really don’t understand how the loop works within the lambda function. Is x the specific text from a value in the question column and why is the all function necessary. Doesnt the all function take in an iterable? If anyone could clear up how this lambda function works that would be greatly appreciated thanks.
Hi I have a question :
To filter out all the strings from the dataframe which contain ‘England’ and ‘King’ I used a different approach… First I made a new column (lower_question) in which I stored all the lower case strings of the question column…and I used the following code to filter the data… However, instead of returning me all the 152 rows it is returning just 106 row. Please tell me what am I doing wrong here:
Okay, I got it. This code was not considering the sentences which had “england’s” or “king’s”. Now my question is that shouldn’t contains(england) and contains(king) take these words into consideration ?
This is the approach I went for too. I don’t have a full technical explanation, though it appears the .contains method does only return results where the specified string is “complete”, that is, surrounded by spaces (unlike “king” in “king’s” or “kings”).
I bypassed that by using the “pipe” - character “|” (which is read as “or”) to include additional strings that contain “king” and “England”. Note that the “|” stays inside the quotation marks:
I agree here. To my memory, that stuff wasn’t taught at all. They did note in the instructions that you’d have to use the internet, but it seemed like the solution code had some excessive extra functionality that wasn’t taught.
all(some_list) is True only if all the boolean values in some_list are True.
For example: all([True, True, True]) is True, but all([True, True, False]) is False.
all(word.lower() in x.lower() for word in words) is the same as all(list) where list is a list comprehension [word.lower() in x.lower() for word in words]. If string x contains all the words from the list words then this list comprehension contains only True values, hence all(word.lower() in x.lower() for word in words) is True.
So filter(x) gives True only if string x contains all the words from list words.
data.loc[data[“some_column”]] choose from your data database rows where boolean values in column “some_column” are True.
So data.loc[data[“Question”].apply(filter)] choose rows from data where string value in column “Question” contains all the words from the list words.
I am using Spyder 4.0 on my system and it is showing error on every line where I have included all_data, it says “name ‘all_data’ is not defined.”
Can one explain what is issue?
Here is the code i came up with to update the columns, change the value of each question to a number and filter the question based on a list of inputs
import pandas as pd
import string
df = pd.read_csv('jeopardy.csv')
df.columns = ['shownum','airdate','round','category','value','question','answer'] #rename columns to variable format
#change value to int64 by removing punctuation and updating none to 0
df['value'] = df['value'].apply(lambda x: x.translate(x.maketrans('','', string.punctuation)) if x != 'None' else 0 ).astype('int64')
def inList(inStr,num):
val = [ ] # creates empty list for True/False response
strMap = inStr.maketrans('','', string.punctuation) # locates punctuation in string
newstr = inStr.translate(strMap) #removes punctuaion in String
quest = newstr.lower().split()
for wrd1 in list:
for i, wrd2 in enumerate(quest,1):
if wrd1.lower() == wrd2:
val.append(True)
break
elif i == len(quest):
val.append(False)
if num == 0:
return any(val)
else:
return all(val)
list = ['baseball','World','Series']
df['inList'] = df.question.apply(lambda str: inList(str,1))
dfFlt = df[df.inList==True]a
resp = dfFlt.answer.unique()
print (resp)
The main part that gave me trouble was the filter function specifically when they used an “all” funciton and .loc. After getting through that it was fun making a simple trivia game that pulls a random question and checks the user input/keeps score.
The lambda function is really easy to understand. First of all we are making all the words from our list in this case words to lower case because if we are looking for the word King and you find king the filter won’t work because they are 2 different strings.
What does our lambda function? Return the boolean value : True if the question contains all the words from our list. In this case x is each question because you are applying the lambda function to the [“Question”] column.