#2.1.10 ★Guided Project: Analyzing Thanksgiving Dinner.md

1. Introducing Thanksgiving Dinner Data

Instructions

  • Import the pandas package.

  • 使用pandas.read_csv()函数来读取thanksgiving.csv
    文件。

  • 确保指定关键字参数encoding="Latin-1",如CSV文件通常不编码。

  • 分配结果的变量data。

  • 显示的前几行data,看看行和列的样子。

  • In a separate notebook cell, display all of the column names to get a sense of what the data consists of.

import pandas as pd
data = pd.read_csv("thanksgiving.csv", encoding="Latin-1")
data.head()
data.columns()

3. Using value_counts To Explore Main Dishes

input
print(data['What is typically the main dish at your Thanksgiving dinner?'].value_counts())
output
Turkey 859Other (please specify) 35Ham/Pork 29Tofurkey 20Chicken 12Roast beef 11I don't know 5Turducken 3Name: What is typically the main dish at your Thanksgiving dinner?, dtype: int64

4. Figuring Out What Pies People Eat

input
apple_isnull = pd.isnull(data['Which type of pie is typically served at your Thanksgiving dinner? Please select all that apply. - Apple'])
pumpkin_isnull = pd.isnull(data['Which type of pie is typically served at your Thanksgiving dinner? Please select all that apply. - Pumpkin'])
pecan_isnull = pd.isnull(data['Which type of pie is typically served at your Thanksgiving dinner? Please select all that apply. - Pecan'])
ate_pies = apple_isnull & pumpkin_isnull & pecan_isnull
print(ate_pies.value_counts())
output
False 876True 182dtype: int64
# 说明有182个选项就没有选择三者pie的任意一种

5. Converting Age To Numeric

input

print(data['Age'].value_counts())

output

45 - 59 28660+ 26430 - 44 25918 - 29 216Name: Age, dtype: int64

input

def str_to_int(age_str):

    if pd.isnull(age_str):    # Use the isnull() function to check if the value is null. If it is, return None.

        return None

    age_str = age_str.split(' ')[0]# Split the string on the space character (), and extract the first item of the resulting list.

    age_str = age_str.replace('+', '') # Replace the + character in the result with an empty string to remove it.

    return int(age_str) # Use int() to convert the result to an integer.

data['int_age'] = data['Age'].apply(str_to_int) # Use the pandas.Series.apply() method to apply the function to each value in the Age column of data.

data['int_age'].describe() # Call the pandas.Series.describe() method on the int_age column of data, and display the result.

output

count 1025.000000mean 39.383415std 15.398493min 18.00000025% 30.00000050% 45.00000075% 60.000000max 60.000000Name: int_age, dtype: float64

6. Converting Income To Numeric

input
print(data['How much total combined money did all members of your HOUSEHOLD earn last year?'].value_counts())
output
$25,000 to $49,999 180Prefer not to answer 136$50,000 to $74,999 135$75,000 to $99,999 133$100,000 to $124,999 111$200,000 and up 80$10,000 to $24,999 68$0 to $9,999 66$125,000 to $149,999 49$150,000 to $174,999 40$175,000 to $199,999 27Name: How much total combined money did all members of your HOUSEHOLD earn last year?, dtype: int64
input
def income_to_int(income_str):
    if pd.isnull(income_str):  # Use the isnull() function to check if the value is null. If it is, return None.
        return None
    income_str = income_str.split(' ')[0] # Split the string on the space character (), and extract the first item of the resulting list.
    if income_str == 'Prefer':
        return None
    income_str = income_str.replace('$', '')
    income_str = income_str.replace(',', '')
    return int(income_str)

data['int_income'] = data['How much total combined money did all members of your HOUSEHOLD earn last year?'].apply(income_to_int)
print(data['int_income'].describe())
output
count 889.000000mean 74077.615298std 59360.742902min 0.00000025% 25000.00000050% 50000.00000075% 100000.000000max 200000.000000Name: int_income, dtype: float64

7. Correlating Travel Distance And Income

input
print(data[data['int_income'] < 150000]['How far will you travel for Thanksgiving?'].value_counts())
print('--------------------------------------------------')
print(data[data['int_income'] > 150000]['How far will you travel for Thanksgiving?'].value_counts())
output
Thanksgiving is happening at my home--I won't travel at all 281Thanksgiving is local--it will take place in the town I live in 203Thanksgiving is out of town but not too far--it's a drive of a few hours or less 150Thanksgiving is out of town and far away--I have to drive several hours or fly 55Name: How far will you travel for Thanksgiving?, dtype: int64--------------------------------------------------Thanksgiving is happening at my home--I won't travel at all 49Thanksgiving is local--it will take place in the town I live in 25Thanksgiving is out of town but not too far--it's a drive of a few hours or less 16Thanksgiving is out of town and far away--I have to drive several hours or fly 12Name: How far will you travel for Thanksgiving?, dtype: int64

8. Linking Friendship And Age

input
data.pivot_table(
    index = "Have you ever tried to meet up with hometown friends on Thanksgiving night?",
    columns = 'Have you ever attended a "Friendsgiving?"',
    values = 'int_age'
)
output

[图片上传中。。。(1)]#####input

data.pivot_table(
    index = 'Have you ever tried to meet up with hometown friends on Thanksgiving night?',
    columns = 'Have you ever attended a "Friendsgiving?"',
    values = 'int_income'
)
output

[图片上传中。。。(2)]

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 202,607评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,047评论 2 379
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 149,496评论 0 335
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,405评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,400评论 5 364
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,479评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,883评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,535评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,743评论 1 295
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,544评论 2 319
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,612评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,309评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,881评论 3 306
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,891评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,136评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,783评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,316评论 2 342

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,331评论 0 23
  • 2016年2月14日,广州到北京,途经郑州。 火车缓缓地驶入郑州站,作短暂的停留。 我蜷缩在床上, 紧紧闭着双眼。...
    冬青夏宁阅读 479评论 0 1
  • 人脉不是你认识的人有多少个,而是有多少人认识你,关键在于认识你的人之中有多少人认可你,你的存在对于他人有意义,他人...
    侨耀中华阅读 120评论 0 0
  • 亲爱的老爸: 你好吗? 老妈今天做了核磁共振,结果显示确系复发,胆管处有个小拳头大小的瘤子,已经是晚期了… 医生决...
    老爸我很想你阅读 130评论 0 0
  • Deco9Zeng阅读 137评论 0 1