<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Thanksgiving Survey Analysis
Every year Thanksgiving is celebrated in United States all around the country. Some people travel to their hometown while others celebrate with friends. In this project, we are going to analyse a survey on Thanksgiving and try to draw some conclusion based on given information.
The dataset used in this analysis is a courtesy of FiveThirtyEight, it is released to public and can be found on their GitHub page.
Structure of the Dataset
The dataset has numerous columns that stand for every question asked in the survey. As explained on FiveThirtyEight's GitHub page (link given above), the columns are
Step1:  Hypothesis 1 - "The most preferred food for Thanksgiving is turkey."
First, let's get rid of rows that answered "No" when asked if they celebrate Thanksgiving
Step2:  Let's look at all unique answers given for main dish at Thanksgiving
Step3:  A short code can show us which food is the most preferred one
Step4:  So as hypothesized, turkey is the most preferred main dish at Thanksgiving dinner.
Hypothesis 2 - "Younger people would travel for Thanksgiving more than older people."
Let's look first few rows of the age column of the data.
Step5:  As can be seen above, the age column has intervals instead of actual numbers. The unique answers are
Step6:  Let's define a function and apply it to "Age" column to cast each answer to a number. (We'll take the average of intervals, and 70 for "60+".)
Step7:  We need to get rid of missing values
Step8:  Now we have ages, let's look at another survey question about traveling for Thanksgiving.
Step9:  Since there are only 4 unique answers, we can calculate a mean age value for each of them.
Step10:  Now, let's plot the results to get a better understanding.
Step11:  As we can see average age of people who stay home is larger than the ones who travel. However, the mean values of ages are pretty close to each other, so we can't say there is a strong correlation between age and travel distance.
Hypothesis 3 
Step12:  Let's look at how income data is stored in the dataset.
Step13:  Again, we have intervals of values instead of precise values. Let's define a function to get an average, (we will have 250000 for "$200,000 and up").
Step14:  We need to get rid of the rows with missing values.
Step15:  We will follow the same process that we did for hypothesis 2.
Step16:  Let's plot the results
<ASSISTANT_TASK:>
Python Code:
# this line is required to see visualizations inline for Jupyter notebook
%matplotlib inline
# importing modules that we need for analysis
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
# read the data from file and print out first few rows and columns
thanksgiving = pd.read_csv("thanksgiving.csv", encoding="Latin-1")
thanksgiving.iloc[0:10,0:3]
thanksgiving.columns[:10]
Explanation: Thanksgiving Survey Analysis
Every year Thanksgiving is celebrated in United States all around the country. Some people travel to their hometown while others celebrate with friends. In this project, we are going to analyse a survey on Thanksgiving and try to draw some conclusion based on given information.
The dataset used in this analysis is a courtesy of FiveThirtyEight, it is released to public and can be found on their GitHub page.
Structure of the Dataset
The dataset has numerous columns that stand for every question asked in the survey. As explained on FiveThirtyEight's GitHub page (link given above), the columns are:
Do you celebrate Thanksgiving?
What is typically the main dish at your Thanksgiving dinner?  
Other (please specify)
How is the main dish typically cooked?
Other (please specify)
What kind of stuffing/dressing do you typically have? 
Other (please specify)
What type of cranberry sauce do you typically have? 
Other (please specify)
Do you typically have gravy?
Which of these side dishes are typically served at your Thanksgiving dinner? Please select all that apply.
Brussel sprouts
Carrots
Cauliflower
Corn
Cornbread
Fruit salad
Green beans/green bean casserole
Macaroni and cheese
Mashed potatoes
Rolls/biscuits
Vegetable salad
Yams/sweet potato casserole
Other (please specify)
Which type of pie is typically served at your Thanksgiving dinner? Please select all that apply. 
Apple
Buttermilk
Cherry
Chocolate
Coconut cream
Key lime
Peach
Pecan
Pumpkin
Sweet Potato
None 
Other (please specify)
Which of these desserts do you typically have at Thanksgiving dinner? Please select all that apply.
Apple cobbler
Blondies
Brownies
Carrot cake
Cheesecake
Cookies
Fudge
Ice cream
Peach cobbler
None
Other (please specify)
Do you typically pray before or after the Thanksgiving meal?
How far will you travel for Thanksgiving?
Will you watch any of the following programs on Thanksgiving? Please select all that apply.  
Macy's Parade
What's the age cutoff at your "kids' table" at Thanksgiving?
Have you ever tried to meet up with hometown friends on Thanksgiving night?
Have you ever attended a "Friendsgiving?"
Will you shop any Black Friday sales on Thanksgiving Day?
Do you work in retail?
Will you employer make you work on Black Friday?
How would you describe where you live? 
Age
What is your gender?
How much total combined money did all members of your HOUSEHOLD earn last year?
US Region
Hypotheses
Before diving into analysis of the data let's come up with hypotheses. Considering the traditions of Thanksgiving, we can say:
The most preferred food for Thanksgiving is turkey. 
Younger people would travel (to their parents' house) for Thanksgiving more than older people.
People with higher income would travel more for Thanksgiving.
Setting up Data
End of explanation
thanksgiving["Do you celebrate Thanksgiving?"].value_counts()
thanksgiving = thanksgiving[thanksgiving["Do you celebrate Thanksgiving?"] == "Yes"]
thanksgiving["Do you celebrate Thanksgiving?"].value_counts()
Explanation: Hypothesis 1 - "The most preferred food for Thanksgiving is turkey."
First, let's get rid of rows that answered "No" when asked if they celebrate Thanksgiving:
End of explanation
thanksgiving["What is typically the main dish at your Thanksgiving dinner?"].unique()
Explanation: Let's look at all unique answers given for main dish at Thanksgiving:
End of explanation
thanksgiving["What is typically the main dish at your Thanksgiving dinner?"].value_counts()
Explanation: A short code can show us which food is the most preferred one:
End of explanation
thanksgiving["Age"][:10]
Explanation: So as hypothesized, turkey is the most preferred main dish at Thanksgiving dinner.
Hypothesis 2 - "Younger people would travel for Thanksgiving more than older people."
Let's look first few rows of the age column of the data.
End of explanation
thanksgiving["Age"].unique()
Explanation: As can be seen above, the age column has intervals instead of actual numbers. The unique answers are:
End of explanation
def age_to_num(string):
    # if nan, return None
    if pd.isnull(string):
        return None
    
    first_item = string.split(" ")[0]
    
    # if the answer is "60+" return 70
    if "+" in first_item:
        return 70.0
    
    last_item = string.split(" ")[2]
    
    #return average of the interval
    return (int(first_item)+int(last_item))/2
# apply age_to_num function to "Age" column and assign it to new column
thanksgiving["num_age"] = thanksgiving["Age"].apply(age_to_num)
thanksgiving["num_age"].unique()
Explanation: Let's define a function and apply it to "Age" column to cast each answer to a number. (We'll take the average of intervals, and 70 for "60+".)
End of explanation
thanksgiving = thanksgiving[thanksgiving["num_age"].isnull() == False]
thanksgiving["num_age"].describe()
Explanation: We need to get rid of missing values:
End of explanation
thanksgiving["How far will you travel for Thanksgiving?"].unique()
Explanation: Now we have ages, let's look at another survey question about traveling for Thanksgiving.
End of explanation
# for each unique answer, select the rows and calculate the mean value of ages.
local_string = 'Thanksgiving is local--it will take place in the town I live in'
local_rows = thanksgiving[thanksgiving["How far will you travel for Thanksgiving?"] == local_string]
local_age_mean = local_rows["num_age"].mean()
fewhours_string = "Thanksgiving is out of town but not too far--it's a drive of a few hours or less"
fewhours_rows = thanksgiving[thanksgiving["How far will you travel for Thanksgiving?"] == fewhours_string]
fewhours_age_mean = fewhours_rows["num_age"].mean()
home_string = "Thanksgiving is happening at my home--I won't travel at all"
home_rows = thanksgiving[thanksgiving["How far will you travel for Thanksgiving?"] == home_string]
home_age_mean = home_rows["num_age"].mean()
faraway_string = 'Thanksgiving is out of town and far away--I have to drive several hours or fly'
faraway_rows = thanksgiving[thanksgiving["How far will you travel for Thanksgiving?"] == faraway_string]
faraway_age_mean = faraway_rows["num_age"].mean()
print("Local: " + str(local_age_mean))
print("Drive of few hours or less: " + str(fewhours_age_mean))
print("Home: " + str(home_age_mean))
print("Drive of several hours or have to fly: " + str(faraway_age_mean))
Explanation: Since there are only 4 unique answers, we can calculate a mean age value for each of them.
End of explanation
x = np.arange(4)+0.75
plt.bar(x,[ fewhours_age_mean, local_age_mean, faraway_age_mean, home_age_mean], width=0.5)
plt.xticks([1,2,3,4], ["Few hours", "Local", "Far away", "Home"])
plt.title("Average Age of People for Different Amounts of Travel on Thanksgiving")
plt.ylabel("Average Age")
plt.xlabel("Travel amount")
plt.show()
Explanation: Now, let's plot the results to get a better understanding.
End of explanation
thanksgiving2 = pd.read_csv("thanksgiving.csv", encoding="Latin-1")
thanksgiving2 = thanksgiving2 = thanksgiving2[thanksgiving2["Do you celebrate Thanksgiving?"] == "Yes"]
Explanation: As we can see average age of people who stay home is larger than the ones who travel. However, the mean values of ages are pretty close to each other, so we can't say there is a strong correlation between age and travel distance.
Hypothesis 3 : "People with higher income would travel more for Thanksgiving."
First, let's read values from file again, since we removed some rows on previous analysis.
End of explanation
thanksgiving2["How much total combined money did all members of your HOUSEHOLD earn last year?"].unique()
Explanation: Let's look at how income data is stored in the dataset.
End of explanation
def income_to_num(string):
    if pd.isnull(string):
        return None
    
    first_item = string.split(" ")[0]
    
    # if the answer is "Prefer not to answer" return none
    if first_item == "Prefer":
        return None
    
    last_item = string.split(" ")[2]
    
    #if the answer is "$200,000 and up" return 250000
    if last_item == "up":
        return 250000.0
    
    #remove dollar signs and commas
    first_item = first_item.replace("$","")
    first_item = first_item.replace(",","")
    last_item = last_item.replace("$","")
    last_item = last_item.replace(",","")
    
    #return the average of the interval
    return (int(first_item)+int(last_item))/2
thanksgiving2["num_income"] = thanksgiving2["How much total combined money did all members of your HOUSEHOLD earn last year?"].apply(income_to_num)
Explanation: Again, we have intervals of values instead of precise values. Let's define a function to get an average, (we will have 250000 for "$200,000 and up").
End of explanation
thanksgiving2 = thanksgiving2[thanksgiving2["num_income"].isnull() == False]
thanksgiving2["num_income"].describe()
Explanation: We need to get rid of the rows with missing values.
End of explanation
# for each unique answer, select the rows and calculate the mean value of income.
local_string = 'Thanksgiving is local--it will take place in the town I live in'
local_rows = thanksgiving2[thanksgiving2["How far will you travel for Thanksgiving?"] == local_string]
local_income_mean = local_rows["num_income"].mean()
fewhours_string = "Thanksgiving is out of town but not too far--it's a drive of a few hours or less"
fewhours_rows = thanksgiving2[thanksgiving2["How far will you travel for Thanksgiving?"] == fewhours_string]
fewhours_income_mean = fewhours_rows["num_income"].mean()
home_string = "Thanksgiving is happening at my home--I won't travel at all"
home_rows = thanksgiving2[thanksgiving2["How far will you travel for Thanksgiving?"] == home_string]
home_income_mean = home_rows["num_income"].mean()
faraway_string = 'Thanksgiving is out of town and far away--I have to drive several hours or fly'
faraway_rows = thanksgiving2[thanksgiving2["How far will you travel for Thanksgiving?"] == faraway_string]
faraway_income_mean = faraway_rows["num_income"].mean()
print("Local: " + str(local_income_mean))
print("Drive of few hours or less: " + str(fewhours_income_mean))
print("Home: " + str(home_income_mean))
print("Drive of several hours or have to fly: " + str(faraway_income_mean))
Explanation: We will follow the same process that we did for hypothesis 2.
End of explanation
x = np.arange(4)+0.75
plt.bar(x,[ fewhours_income_mean, local_income_mean, faraway_income_mean, home_income_mean], width=0.5)
plt.xticks([1,2,3,4], ["Few hours", "Local", "Far away", "Home"])
plt.title("Average Income of People for Different Amounts of Travel on Thanksgiving")
plt.ylabel("Average Income")
plt.xlabel("Travel amount")
plt.show()
Explanation: Let's plot the results:
End of explanation
<END_TASK>