This is Jeopardy Challenge Project (Python, Pandas)

Congratulations on completing your project!

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:

  1. 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.
  2. 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.

2 Likes

The function all, the method .lower and .loc was not very well taught in the course.

8 Likes

Yeah, I struggle understanding this as well

1 Like

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:

df[‘lower_question’] = df.question.str.lower()
filtered1 = df[df[‘lower_question’].str.contains(‘england’) & df[‘question’].str.contains(‘king’)]

1 Like

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 ?

1 Like

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:

jepardah[(jepardah[“Question”].str.contains(“King|king”))&
(jepardah[“Question”].str.contains(“England”))]

This returns 152 rows though I realise it would start looking a bit clumsy if we include lots more variations like “king’s”, “King’s” etc

1 Like

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.

I agree, that needs explanation. Can anyone explain?

If I understood correctly:

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.

4 Likes

I used regular expression \bsome_word\b to find separated word in a string. It works fine with cases like "England’s"

2 Likes

Thank you for the very good explanation. Appreciate it!

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?

Hey guys, here’s my version of a game. It doesn’t follow the rules of the real game, it’s rather a practice project based on a tutorial.
https://github.com/ikatar/practice/tree/master/Jeopardy_game

1 Like

Found a simple way to clean out from question column a lot of trash like this:

<a href="http://www.j-archive.com/media/2007-04-25_DJ_25.jpg" target="_blank">
<a href="http://www.j-archive.com/media/2007-04-25_DJ_26.jpg" target="_blank">
<a href="http://www.j-archive.com/media/2007-04-25_DJ_27.jpg" target="_blank">
<a href="http://www.j-archive.com/media/2007-04-25_DJ_28.jpg" target="_blank">

Super useful Regex!

jeopardy['question'] = jeopardy.question.apply(lambda x: re.sub(r"<[^<>]*>", "", x))
1 Like
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)

I completed the project and here is the link to view it on github.
https://github.com/johngutentag/jeopardy

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.

1 Like

Hello corwhiz38760,

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.

This is my project

I’ve really enjoyed this project!!!
Have fun with my quiz!

Your explanation using list comprehension was what I needed to understand this. In the end, it’s very simple when we can see through the syntax.

1 Like