本书正文的最后一章,我们来看一些真实世界的数据集。对于每个数据集,我们会用之前介绍的方法,从原始数据中提取有意义的内容。展示的方法适用于其它数据集,也包括你的。本章包含了一些各种各样的案例数据集,可以用来练习。

案例数据集可以在Github仓库找到,见第一章。

来自Bitly的USA.gov数据

2011年,URL缩短服务Bitly跟美国政府网站USA.gov合作,提供了一份从生成.gov或.mil短链接的用户那里收集来的匿名数据。在2011年,除实时数据之外,还可以下载文本文件形式的每小时快照。写作此书时(2017年),这项服务已经关闭,但我们保存一份数据用于本书的案例。

以每小时快照为例,文件中各行的格式为JSON(即JavaScript Object Notation,这是一种常用的Web数据格式)。例如,如果我们只读取某个文件中的第一行,那么所看到的结果应该是下面这样:

path = 'datasets/bitly_usagov/example.txt'
open(path).readline()
'{ "a": "Mozilla\\/5.0 (Windows NT 6.1; WOW64) AppleWebKit\\/535.11 (KHTML, like Gecko) Chrome\\/17.0.963.78 Safari\\/535.11", "c": "US", "nk": 1, "tz": "America\\/New_York", "gr": "MA", "g": "A6qOVH", "h": "wfLQtf", "l": "orofrog", "al": "en-US,en;q=0.8", "hh": "1.usa.gov", "r": "http:\\/\\/www.facebook.com\\/l\\/7AQEFzjSi\\/1.usa.gov\\/wfLQtf", "u": "http:\\/\\/www.ncbi.nlm.nih.gov\\/pubmed\\/22415991", "t": 1331923247, "hc": 1331822918, "cy": "Danvers", "ll": [ 42.576698, -70.954903 ] }\n'

Python有内置或第三方模块可以将JSON字符串转换成Python字典对象。这里,我将使用json模块及其loads函数逐行加载已经下载好的数据文件:

import json
path = 'datasets/bitly_usagov/example.txt'
records = [json.loads(line) for line in open(path)]

现在,records对象就成为一组Python字典了:

records[0]
{'a': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.78 Safari/535.11','c': 'US','nk': 1,'tz': 'America/New_York','gr': 'MA','g': 'A6qOVH','h': 'wfLQtf','l': 'orofrog','al': 'en-US,en;q=0.8','hh': '1.usa.gov','r': 'http://www.facebook.com/l/7AQEFzjSi/1.usa.gov/wfLQtf','u': 'http://www.ncbi.nlm.nih.gov/pubmed/22415991','t': 1331923247,'hc': 1331822918,'cy': 'Danvers','ll': [42.576698, -70.954903]}

用纯Python代码对时区进行计数

假设我们想要知道该数据集中最常出现的是哪个时区(即tz字段),得到答案的办法有很多。首先,我们用列表推导式取出一组时区:

time_zones = [rec['tz'] for rec in records]
---------------------------------------------------------------------------KeyError                                  Traceback (most recent call last)<ipython-input-5-f3fbbc37f129> in <module>
----> 1 time_zones = [rec['tz'] for rec in records]<ipython-input-5-f3fbbc37f129> in <listcomp>(.0)
----> 1 time_zones = [rec['tz'] for rec in records]KeyError: 'tz'

晕!原来并不是所有记录都有时区字段。这个好办,只需在列表推导式末尾加上一个if 'tz’in rec判断即可:

time_zones = [rec['tz'] for rec in records if 'tz' in rec]
time_zones[:9]

只看前10个时区,我们发现有些是未知的(即空的)。虽然可以将它们过滤掉,但现在暂时先留着。接下来,为了对时区进行计数,这里介绍两个办法:一个较难(只使用标准Python库),另一个较简单(使用pandas)。计数的办法之一是在遍历时区的过程中将计数值保存在字典中:

def get_counts(sequence):counts = {}for x in sequence:if x in counts:counts[x] += 1else:counts[x] = 1return counts

如果使用Python标准库的更高级工具,那么你可能会将代码写得更简洁一些:

from collections import defaultdictdef get_counts2(sequence):counts = defaultdict(int) # values will initialize to 0for x in sequence:counts[x] += 1return counts
seq = 'xxxss'
res = get_counts2(seq)
res
res['d']

我将逻辑写到函数中是为了获得更高的复用性。要用它对时区进行处理,只需将time_zones传入即可:

counts = get_counts(time_zones)
counts['America/New_York']
len(time_zones)

如果想要得到前10位的时区及其计数值,我们需要用到一些有关字典的处理技巧:

def top_counts(count_dict, n=10):value_key_pairs = [(count, tz) for tz, count in count_dict.items()]value_key_pairs.sort()return value_key_pairs[-n:]

然后有:

top_counts(counts)

如果你搜索Python的标准库,你能找到collections.Counter类,它可以使这项工作更简单:

from collections import Countercounts = Counter(time_zones)
counts.most_common(10)

用pandas对时区进行计数

从原始记录的集合创建DateFrame,与将记录列表传递到pandas.DataFrame一样简单:

import pandas as pdframe = pd.DataFrame(records)frame.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3560 entries, 0 to 3559
Data columns (total 18 columns):
_heartbeat_    120 non-null float64
a              3440 non-null object
al             3094 non-null object
c              2919 non-null object
cy             2919 non-null object
g              3440 non-null object
gr             2919 non-null object
h              3440 non-null object
hc             3440 non-null float64
hh             3440 non-null object
kw             93 non-null object
l              3440 non-null object
ll             2919 non-null object
nk             3440 non-null float64
r              3440 non-null object
t              3440 non-null float64
tz             3440 non-null object
u              3440 non-null object
dtypes: float64(4), object(14)
memory usage: 500.7+ KB
frame['tz'][:10]

这里frame的输出形式是摘要视图(summary view),主要用于较大的DataFrame对象。我们然后可以对Series使用value_counts方法:

tz_counts = frame['tz'].value_counts()
tz_counts[:10]

我们可以用matplotlib可视化这个数据。为此,我们先给记录中未知或缺失的时区填上一个替代值。fillna函数可以替换缺失值(NA),而未知值(空字符串)则可以通过布尔型数组索引加以替换:

clean_tz = frame['tz'].fillna('Missing')clean_tz[clean_tz == ''] = 'Unknown'tz_counts = clean_tz.value_counts()
tz_counts[:10]

此时,我们可以用seaborn包创建水平柱状图(结果见图14-1)

import seaborn as sns
subset = tz_counts[:10]
sns.barplot(y=subset.index, x=subset.values)
sns.barplot(y=subset.values, x=subset.index)

a字段含有执行URL短缩操作的浏览器、设备、应用程序的相关信息:

frame['a'].head()
frame['a'][50]
frame['a'][1]
frame['a'][51][:50]  # long line

将这些"agent"字符串中的所有信息都解析出来是一件挺郁闷的工作。一种策略是将这种字符串的第一节(与浏览器大致对应)分离出来并得到另外一份用户行为摘要:

results = pd.Series([x.split()[0] for x in frame.a.dropna()])results[:5]
results.value_counts()[:8]

现在,假设你想按Windows和非Windows用户对时区统计信息进行分解。为了简单起见,我们假定只要agent字符串中含有"Windows"就认为该用户为Windows用户。由于有的agent缺失,所以首先将它们从数据中移除:

cframe = frame[frame.a.notnull()]

然后计算出各行是否含有Windows的值:

cframe['os'] = np.where(cframe['a'].str.contains('Windows'),'Windows', 'Not Windows')
cframe['os'].head(10)
import re
os = [ 'Windows' if re.search('.*Windows.*',i) else 'Not Windows'  for i in cframe['a'] ]
start = 0
for index ,c in enumerate(os):print(index , c)start += 1if start > 9:break

接下来就可以根据时区和新得到的操作系统列表对数据进行分组了:

by_tz_os = cframe.groupby(['tz', 'os'])

分组计数,类似于value_counts函数,可以用size来计算。并利用unstack对计数结果进行重塑:

agg_counts = by_tz_os.size().unstack().fillna(0)
agg_counts[:10]

最后,我们来选取最常出现的时区。为了达到这个目的,我根据agg_counts中的行数构造了一个间接索引数组:

indexer = agg_counts.sum(1)
indexer[:5]
indexer = agg_counts.sum(1).argsort()
indexer[:5]

然后我通过take按照这个顺序截取了最后10行最大值:

count_subset = agg_counts.take(indexer[-10:])
count_subset

pandas有一个简便方法nlargest,可以做同样的工作:

agg_counts.sum(1).nlargest(10)

然后,如这段代码所示,可以用柱状图表示。我传递一个额外参数到seaborn的barpolt函数,来画一个堆积条形图(见图14-2):

count_subset[:4]
# Rearrange the data for plotting
count_subset = count_subset.stack()
count_subset[:4]
count_subset.name = 'total'
count_subset[:4]
count_subset = count_subset.reset_index()
count_subset[:4]
sns.barplot(x='total', y='tz', hue='os',  data=count_subset)

这张图不容易看出Windows用户在小分组中的相对比例,因此标准化分组百分比之和为1:

def norm_total(group):group['normed_total'] = group.total / group.total.sum()return groupresults = count_subset.groupby('tz').apply(norm_total)
results.head()

再次画图,见图14-3:

sns.barplot(x='normed_total', y='tz', hue='os',  data=results)

我们还可以用groupby的transform方法,更高效的计算标准化的和:

g = count_subset.groupby('tz')results2 = count_subset.total / g.total.transform('sum')

MovieLens 1M数据集

GroupLens Research(http://www.grouplens.org/node/73) 采集了一组从20世纪90年末到21世纪初由MovieLens用户提供的电影评分数据。这些数据中包括电影评分、电影元数据(风格类型和年代)以及关于用户的人口统计学数据(年龄、邮编、性别和职业等)。基于机器学习算法的推荐系统一般都会对此类数据感兴趣。虽然我不会在本书中详细介绍机器学习技术,但我会告诉你如何对这种数据进行切片切块以满足实际需求。

MovieLens 1M数据集含有来自6000名用户对4000部电影的100万条评分数据。它分为三个表:评分、用户信息和电影信息。将该数据从zip文件中解压出来之后,可以通过pandas.read_table将各个表分别读到一个pandas DataFrame对象中:

import pandas as pd# Make display smaller
pd.options.display.max_rows = 10unames = ['user_id', 'gender', 'age', 'occupation', 'zip']
users = pd.read_table('datasets/movielens/users.dat', sep='::',header=None, names=unames)rnames = ['user_id', 'movie_id', 'rating', 'timestamp']
ratings = pd.read_table('datasets/movielens/ratings.dat', sep='::',header=None, names=rnames)
mnames = ['movie_id', 'title', 'genres']
movies = pd.read_table('datasets/movielens/movies.dat', sep='::',header=None, names=mnames)
/root/miniconda3/lib/python3.6/site-packages/ipykernel_launcher.py:8: ParserWarning: Falling back to the 'python' engine because the 'c' engine does not support regex separators (separators > 1 char and different from '\s+' are interpreted as regex); you can avoid this warning by specifying engine='python'./root/miniconda3/lib/python3.6/site-packages/ipykernel_launcher.py:12: ParserWarning: Falling back to the 'python' engine because the 'c' engine does not support regex separators (separators > 1 char and different from '\s+' are interpreted as regex); you can avoid this warning by specifying engine='python'.if sys.path[0] == '':
/root/miniconda3/lib/python3.6/site-packages/ipykernel_launcher.py:15: ParserWarning: Falling back to the 'python' engine because the 'c' engine does not support regex separators (separators > 1 char and different from '\s+' are interpreted as regex); you can avoid this warning by specifying engine='python'.from ipykernel import kernelapp as app

利用Python的切片语法,通过查看每个DataFrame的前几行即可验证数据加载工作是否一切顺利:

users[:5]
ratings[:5]
movies[:5]
ratings

注意,其中的年龄和职业是以编码形式给出的,它们的具体含义请参考该数据集的README文件。分析散布在三个表中的数据可不是一件轻松的事情。假设我们想要根据性别和年龄计算某部电影的平均得分,如果将所有数据都合并到一个表中的话问题就简单多了。我们先用pandas的merge函数将ratings跟users合并到一起,然后再将movies也合并进去。pandas会根据列名的重叠情况推断出哪些列是合并(或连接)键:

data = pd.merge(pd.merge(ratings, users), movies)
data
data.iloc[0]
mean_ratings = data.pivot_table('rating', index='title',columns='gender', aggfunc='mean')
mean_ratings[:5]

该操作产生了另一个DataFrame,其内容为电影平均得分,行标为电影名称(索引),列标为性别。现在,我打算过滤掉评分数据不够250条的电影(随便选的一个数字)。为了达到这个目的,我先对title进行分组,然后利用size()得到一个含有各电影分组大小的Series对象:

ratings_by_title = data.groupby('title').size()
ratings_by_title[:10]
active_titles = ratings_by_title.index[ratings_by_title >= 250]
active_titles

标题索引中含有评分数据大于250条的电影名称,然后我们就可以据此从前面的mean_ratings中选取所需的行了:

# Select rows on the index
mean_ratings = mean_ratings.loc[active_titles]mean_ratings

为了了解女性观众最喜欢的电影,我们可以对F列降序排列:

top_female_ratings = mean_ratings.sort_values(by='F', ascending=False)
top_female_ratings[:10]

计算评分分歧

假设我们想要找出男性和女性观众分歧最大的电影。一个办法是给mean_ratings加上一个用于存放平均得分之差的列,并对其进行排序:

mean_ratings['diff'] = mean_ratings['M'] - mean_ratings['F']

按"diff"排序即可得到分歧最大且女性观众更喜欢的电影:

sorted_by_diff = mean_ratings.sort_values(by='diff')
sorted_by_diff[:10]

对排序结果反序并取出前10行,得到的则是男性观众更喜欢的电影:

# Reverse order of rows, take first 10 rows
sorted_by_diff[::-1][:10]

如果只是想要找出分歧最大的电影(不考虑性别因素),则可以计算得分数据的方差或标准差:

rating_std_by_title = data.groupby('title')['rating'].std()
rating_std_by_title = rating_std_by_title.loc[active_titles]
rating_std_by_title.sort_values(ascending=False)[:10]

可能你已经注意到了,电影分类是以竖线(|)分隔的字符串形式给出的。如果想对电影分类进行分析的话,就需要先将其转换成更有用的形式才行。

1880-2010年间全美婴儿姓名

美国社会保障总署(SSA)提供了一份从1880年到现在的婴儿名字频率数据。Hadley Wickham(许多流行R包的作者)经常用这份数据来演示R的数据处理功能。

我们要做一些数据规整才能加载这个数据集,这么做就会产生一个如下的DataFrame:

In [4]: names.head(10)
Out[4]:name sex  births  year
0       Mary   F    7065  1880
1       Anna   F    2604  1880
2       Emma   F    2003  1880
3  Elizabeth   F    1939  1880
4     Minnie   F    1746  1880
5   Margaret   F    1578  1880
6        Ida   F    1472  1880
7      Alice   F    1414  1880
8     Bertha   F    1320  1880
9      Sarah   F    1288  1880

你可以用这个数据集做很多事,例如:

  • 计算指定名字(可以是你自己的,也可以是别人的)的年度比例。
  • 计算某个名字的相对排名。
  • 计算各年度最流行的名字,以及增长或减少最快的名字。
  • 分析名字趋势:元音、辅音、长度、总体多样性、拼写变化、首尾字母等。
  • 分析外源性趋势:圣经中的名字、名人、人口结构变化等。

利用前面介绍过的那些工具,这些分析工作都能很轻松地完成,我会讲解其中的一些。
到编写本书时为止,美国社会保障总署将该数据库按年度制成了多个数据文件,其中给出了每个性别/名字组合的出生总数。这些文件的原始档案可以在这里获取:http://www.ssa.gov/oact/babynames/limits.html。

如果你在阅读本书的时候这个页面已经不见了,也可以用搜索引擎找找。

下载"National data"文件names.zip,解压后的目录中含有一组文件(如yob1880.txt)。我用UNIX的head命令查看了其中一个文件的前10行(在Windows上,你可以用more命令,或直接在文本编辑器中打开):

!head -n 10 datasets/babynames/yob1880.txt

由于这是一个非常标准的以逗号隔开的格式,所以可以用pandas.read_csv将其加载到DataFrame中:

import pandas as pd# Make display smaller
pd.options.display.max_rows = 10names1880 = pd.read_csv('datasets/babynames/yob1880.txt',names=['name', 'sex', 'births'])
names1880
name sex births
0 Mary F 7065
1 Anna F 2604
2 Emma F 2003
3 Elizabeth F 1939
4 Minnie F 1746
... ... ... ...
1995 Woodie M 5
1996 Worthy M 5
1997 Wright M 5
1998 York M 5
1999 Zachariah M 5

2000 rows × 3 columns

这些文件中仅含有当年出现超过5次的名字。为了简单起见,我们可以用births列的sex分组小计表示该年度的births总计:

names1880.groupby('sex').births.sum()
sex
F     90993
M    110493
Name: births, dtype: int64

由于该数据集按年度被分隔成了多个文件,所以第一件事情就是要将所有数据都组装到一个DataFrame里面,并加上一个year字段。使用pandas.concat即可达到这个目的:

years = range(1880, 2011)pieces = []
columns = ['name', 'sex', 'births']for year in years:path = 'datasets/babynames/yob%d.txt' % yearframe = pd.read_csv(path, names=columns)frame['year'] = yearpieces.append(frame)# Concatenate everything into a single DataFrame
names = pd.concat(pieces, ignore_index=True)

这里需要注意几件事情。第一,concat默认是按行将多个DataFrame组合到一起的;第二,必须指定ignore_index=True,因为我们不希望保留read_csv所返回的原始行号。现在我们得到了一个非常大的DataFrame,它含有全部的名字数据:

names
name sex births year
0 Mary F 7065 1880
1 Anna F 2604 1880
2 Emma F 2003 1880
3 Elizabeth F 1939 1880
4 Minnie F 1746 1880
... ... ... ... ...
1690779 Zymaire M 5 2010
1690780 Zyonne M 5 2010
1690781 Zyquarius M 5 2010
1690782 Zyran M 5 2010
1690783 Zzyzx M 5 2010

1690784 rows × 4 columns

有了这些数据之后,我们就可以利用groupby或pivot_table在year和sex级别上对其进行聚合了,如图14-4所示:

total_births = names['births'].groupby([names['year'],names['sex']]).sum()
total_births.unstack('sex').tail()
sex F M
year
2006 1896468 2050234
2007 1916888 2069242
2008 1883645 2032310
2009 1827643 1973359
2010 1759010 1898382
total_births = names.pivot_table('births', index='year',columns='sex', aggfunc=sum)
total_births.tail()
sex F M
year
2006 1896468 2050234
2007 1916888 2069242
2008 1883645 2032310
2009 1827643 1973359
2010 1759010 1898382
#按性别和年度统计的总出生数
total_births.plot(title='Total births by sex and year')
<matplotlib.axes._subplots.AxesSubplot at 0x7f8b347c1048>

下面我们来插入一个prop列,用于存放指定名字的婴儿数相对于总出生数的比例。prop值为0.02表示每100名婴儿中有2名取了当前这个名字。因此,我们先按year和sex分组,然后再将新列加到各个分组上:

def add_prop(group):group['prop'] = group.births / group.births.sum()return group
names = names.groupby(['year', 'sex']).apply(add_prop)
names
name sex births year prop
0 Mary F 7065 1880 0.077643
1 Anna F 2604 1880 0.028618
2 Emma F 2003 1880 0.022013
3 Elizabeth F 1939 1880 0.021309
4 Minnie F 1746 1880 0.019188
... ... ... ... ... ...
1690779 Zymaire M 5 2010 0.000003
1690780 Zyonne M 5 2010 0.000003
1690781 Zyquarius M 5 2010 0.000003
1690782 Zyran M 5 2010 0.000003
1690783 Zzyzx M 5 2010 0.000003

1690784 rows × 5 columns

在执行这样的分组处理时,一般都应该做一些有效性检查,比如验证所有分组的prop的总和是否为1:

names.groupby(['year', 'sex']).prop.sum()
year  sex
1880  F      1.0M      1.0
1881  F      1.0M      1.0
1882  F      1.0...
2008  M      1.0
2009  F      1.0M      1.0
2010  F      1.0M      1.0
Name: prop, Length: 262, dtype: float64

工作完成。为了便于实现更进一步的分析,我需要取出该数据的一个子集:每对sex/year组合的前1000个名字。这又是一个分组操作:

def get_top1000(group):return group.sort_values(by='births', ascending=False)[:1000]
grouped = names.groupby(['year', 'sex'])
top1000 = grouped.apply(get_top1000)
# Drop the group index, not needed
top1000.reset_index(inplace=True, drop=True)

如果你喜欢DIY的话,也可以这样:

pieces = []
for year, group in names.groupby(['year', 'sex']):pieces.append(group.sort_values(by='births', ascending=False)[:1000])
top1000 = pd.concat(pieces, ignore_index=True)

现在的结果数据集就小多了:

top1000
name sex births year prop
0 Mary F 7065 1880 0.077643
1 Anna F 2604 1880 0.028618
2 Emma F 2003 1880 0.022013
3 Elizabeth F 1939 1880 0.021309
4 Minnie F 1746 1880 0.019188
... ... ... ... ... ...
261872 Camilo M 194 2010 0.000102
261873 Destin M 194 2010 0.000102
261874 Jaquan M 194 2010 0.000102
261875 Jaydan M 194 2010 0.000102
261876 Maxton M 193 2010 0.000102

261877 rows × 5 columns

接下来的数据分析工作就针对这个top1000数据集了。

分析命名趋势

有了完整的数据集和刚才生成的top1000数据集,我们就可以开始分析各种命名趋势了。首先将前1000个名字分为男女两个部分:

boys = top1000[top1000.sex == 'M']
girls = top1000[top1000.sex == 'F']

这是两个简单的时间序列,只需稍作整理即可绘制出相应的图表(比如每年叫做John和Mary的婴儿数)。我们先生成一张按year和name统计的总出生数透视表:

total_births = top1000.pivot_table('births', index='year',columns='name',aggfunc=sum)
total_births
name Aaden Aaliyah Aarav Aaron Aarush Ab Abagail Abb Abbey Abbie ... Zoa Zoe Zoey Zoie Zola Zollie Zona Zora Zula Zuri
year
1880 NaN NaN NaN 102.0 NaN NaN NaN NaN NaN 71.0 ... 8.0 23.0 NaN NaN 7.0 NaN 8.0 28.0 27.0 NaN
1881 NaN NaN NaN 94.0 NaN NaN NaN NaN NaN 81.0 ... NaN 22.0 NaN NaN 10.0 NaN 9.0 21.0 27.0 NaN
1882 NaN NaN NaN 85.0 NaN NaN NaN NaN NaN 80.0 ... 8.0 25.0 NaN NaN 9.0 NaN 17.0 32.0 21.0 NaN
1883 NaN NaN NaN 105.0 NaN NaN NaN NaN NaN 79.0 ... NaN 23.0 NaN NaN 10.0 NaN 11.0 35.0 25.0 NaN
1884 NaN NaN NaN 97.0 NaN NaN NaN NaN NaN 98.0 ... 13.0 31.0 NaN NaN 14.0 6.0 8.0 58.0 27.0 NaN
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
2006 NaN 3737.0 NaN 8279.0 NaN NaN 297.0 NaN 404.0 440.0 ... NaN 5145.0 2839.0 530.0 NaN NaN NaN NaN NaN NaN
2007 NaN 3941.0 NaN 8914.0 NaN NaN 313.0 NaN 349.0 468.0 ... NaN 4925.0 3028.0 526.0 NaN NaN NaN NaN NaN NaN
2008 955.0 4028.0 219.0 8511.0 NaN NaN 317.0 NaN 344.0 400.0 ... NaN 4764.0 3438.0 492.0 NaN NaN NaN NaN NaN NaN
2009 1265.0 4352.0 270.0 7936.0 NaN NaN 296.0 NaN 307.0 369.0 ... NaN 5120.0 3981.0 496.0 NaN NaN NaN NaN NaN NaN
2010 448.0 4628.0 438.0 7374.0 226.0 NaN 277.0 NaN 295.0 324.0 ... NaN 6200.0 5164.0 504.0 NaN NaN NaN NaN NaN 258.0

131 rows × 6868 columns

现在,我们用DataFrame的plot方法绘制几个名字的曲线图(见图14-5):

total_births.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 131 entries, 1880 to 2010
Columns: 6868 entries, Aaden to Zuri
dtypes: float64(6868)
memory usage: 6.9 MB
subset = total_births[['John', 'Harry', 'Mary', 'Marilyn']]
subset.plot(subplots=True, figsize=(12, 10), grid=False,title="Number of births per year")
array([<matplotlib.axes._subplots.AxesSubplot object at 0x7f8b33eaabe0>,<matplotlib.axes._subplots.AxesSubplot object at 0x7f8b33c9a588>,<matplotlib.axes._subplots.AxesSubplot object at 0x7f8b33c497f0>,<matplotlib.axes._subplots.AxesSubplot object at 0x7f8b33c6ec50>],dtype=object)

从图中可以看出,这几个名字在美国人民的心目中已经风光不再了。但事实并非如此简单,我们在下一节中就能知道是怎么一回事了。

评估命名多样性的增长

一种解释是父母愿意给小孩起常见的名字越来越少。这个假设可以从数据中得到验证。一个办法是计算最流行的1000个名字所占的比例,我按year和sex进行聚合并绘图(见图14-6):

table = top1000.pivot_table('prop', index='year',columns='sex', aggfunc=sum)
table
sex F M
year
1880 1.000000 0.997375
1881 1.000000 1.000000
1882 0.998702 0.995646
1883 0.997596 0.998566
1884 0.993156 0.994539
... ... ...
2006 0.753153 0.860368
2007 0.745959 0.855159
2008 0.740933 0.850003
2009 0.737290 0.845256
2010 0.736780 0.843156

131 rows × 2 columns

table.plot(title='Sum of table1000.prop by year and sex',yticks=np.linspace(0, 1.2, 13), xticks=range(1880, 2020, 10))
<matplotlib.axes._subplots.AxesSubplot at 0x7f8b33b1cb00>

从图中可以看出,名字的多样性确实出现了增长(前1000项的比例降低)。另一个办法是计算占总出生人数前50%的不同名字的数量,这个数字不太好计算。我们只考虑2010年男孩的名字

df = boys[boys.year == 2010]
df
name sex births year prop
260877 Jacob M 21875 2010 0.011523
260878 Ethan M 17866 2010 0.009411
260879 Michael M 17133 2010 0.009025
260880 Jayden M 17030 2010 0.008971
260881 William M 16870 2010 0.008887
... ... ... ... ... ...
261872 Camilo M 194 2010 0.000102
261873 Destin M 194 2010 0.000102
261874 Jaquan M 194 2010 0.000102
261875 Jaydan M 194 2010 0.000102
261876 Maxton M 193 2010 0.000102

1000 rows × 5 columns

在对prop降序排列之后,我们想知道前面多少个名字的人数加起来才够50%。虽然编写一个for循环确实也能达到目的,但NumPy有一种更聪明的矢量方式。先计算prop的累计和cumsum,然后再通过searchsorted方法找出0.5应该被插入在哪个位置才能保证不破坏顺序:

prop_cumsum = df.sort_values(by='prop', ascending=False).prop.cumsum()
prop_cumsum
260877    0.011523
260878    0.020934
260879    0.029959
260880    0.038930
260881    0.047817...
261872    0.842748
261873    0.842850
261874    0.842953
261875    0.843055
261876    0.843156
Name: prop, Length: 1000, dtype: float64
prop_cumsum.values.searchsorted(0.5)
116

由于数组索引是从0开始的,因此我们要给这个结果加1,即最终结果为117。拿1900年的数据来做个比较,这个数字要小得多:

df = boys[boys.year == 1900]
in1900 = df.sort_values(by='prop', ascending=False).prop.cumsum()
in1900.values.searchsorted(0.5) + 1
25

现在就可以对所有year/sex组合执行这个计算了。按这两个字段进行groupby处理,然后用一个函数计算各分组的这个值:

def get_quantile_count(group, q=0.5):group = group.sort_values(by='prop', ascending=False)return group.prop.cumsum().values.searchsorted(q) + 1diversity = top1000.groupby(['year', 'sex']).apply(get_quantile_count)
diversity = diversity.unstack('sex')

现在,diversity这个DataFrame拥有两个时间序列(每个性别各一个,按年度索引)。通过IPython,你可以查看其内容,还可以像之前那样绘制图表(如图14-7所示):

diversity.head()
sex F M
year
1880 38 14
1881 38 14
1882 38 15
1883 39 15
1884 39 16
diversity.plot(title="Number of popular names in top 50%")
<matplotlib.axes._subplots.AxesSubplot at 0x7f8b33a7f898>

从图中可以看出,女孩名字的多样性总是比男孩的高,而且还在变得越来越高。读者们可以自己分析一下具体是什么在驱动这个多样性(比如拼写形式的变化)。

“最后一个字母”的变革

2007年,一名婴儿姓名研究人员Laura Wattenberg在她自己的网站上指出(http://www.babynamewizard.com) :近百年来,男孩名字在最后一个字母上的分布发生了显著的变化。为了了解具体的情况,我首先将全部出生数据在年度、性别以及末字母上进行了聚合:

# extract last letter from name column
get_last_letter = lambda x: x[-1]
last_letters = names.name.map(get_last_letter)
last_letters.name = 'last_letter'
table = names.pivot_table('births', index=last_letters,columns=['sex', 'year'], aggfunc=sum)
table.head(3)
sex F ... M
year 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 ... 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010
last_letter
a 31446.0 31581.0 36536.0 38330.0 43680.0 45408.0 49100.0 48942.0 59442.0 58631.0 ... 39124.0 38815.0 37825.0 38650.0 36838.0 36156.0 34654.0 32901.0 31430.0 28438.0
b NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ... 50950.0 49284.0 48065.0 45914.0 43144.0 42600.0 42123.0 39945.0 38862.0 38859.0
c NaN NaN 5.0 5.0 NaN NaN NaN NaN NaN NaN ... 27113.0 27238.0 27697.0 26778.0 26078.0 26635.0 26864.0 25318.0 24048.0 23125.0

3 rows × 262 columns

然后,我选出具有一定代表性的三年,并输出前面几行:

subtable = table.reindex(columns=[1910, 1960, 2010], level='year')
subtable.head()
sex F M
year 1910 1960 2010 1910 1960 2010
last_letter
a 108376.0 691247.0 670605.0 977.0 5204.0 28438.0
b NaN 694.0 450.0 411.0 3912.0 38859.0
c 5.0 49.0 946.0 482.0 15476.0 23125.0
d 6750.0 3729.0 2607.0 22111.0 262112.0 44398.0
e 133569.0 435013.0 313833.0 28655.0 178823.0 129012.0

接下来我们需要按总出生数对该表进行规范化处理,以便计算出各性别各末字母占总出生人数的比例:

subtable.sum()
sex  year
F    1910     396416.01960    2022062.02010    1759010.0
M    1910     194198.01960    2132588.02010    1898382.0
dtype: float64
letter_prop = subtable / subtable.sum()
letter_prop
sex F M
year 1910 1960 2010 1910 1960 2010
last_letter
a 0.273390 0.341853 0.381240 0.005031 0.002440 0.014980
b NaN 0.000343 0.000256 0.002116 0.001834 0.020470
c 0.000013 0.000024 0.000538 0.002482 0.007257 0.012181
d 0.017028 0.001844 0.001482 0.113858 0.122908 0.023387
e 0.336941 0.215133 0.178415 0.147556 0.083853 0.067959
... ... ... ... ... ... ...
v NaN 0.000060 0.000117 0.000113 0.000037 0.001434
w 0.000020 0.000031 0.001182 0.006329 0.007711 0.016148
x 0.000015 0.000037 0.000727 0.003965 0.001851 0.008614
y 0.110972 0.152569 0.116828 0.077349 0.160987 0.058168
z 0.002439 0.000659 0.000704 0.000170 0.000184 0.001831

26 rows × 6 columns

有了这个字母比例数据之后,就可以生成一张各年度各性别的条形图了,如图14-8所示:

import matplotlib.pyplot as pltfig, axes = plt.subplots(2, 1, figsize=(10, 8))
letter_prop['M'].plot(kind='bar', rot=0, ax=axes[0], title='Male')
letter_prop['F'].plot(kind='bar', rot=0, ax=axes[1], title='Female',legend=False)
<matplotlib.axes._subplots.AxesSubplot at 0x7f8b33946c50>

可以看出,从20世纪60年代开始,以字母"n"结尾的男孩名字出现了显著的增长。回到之前创建的那个完整表,按年度和性别对其进行规范化处理,并在男孩名字中选取几个字母,最后进行转置以便将各个列做成一个时间序列:

letter_prop = table / table.sum()
dny_ts = letter_prop.loc[['d', 'n', 'y'], 'M'].T
dny_ts.head()
last_letter d n y
year
1880 0.083055 0.153213 0.075760
1881 0.083247 0.153214 0.077451
1882 0.085340 0.149560 0.077537
1883 0.084066 0.151646 0.079144
1884 0.086120 0.149915 0.080405

有了这个时间序列的DataFrame之后,就可以通过其plot方法绘制出一张趋势图了(如图14-9所示):

dny_ts.plot()
<matplotlib.axes._subplots.AxesSubplot at 0x7f8b33705358>

变成女孩名字的男孩名字(以及相反的情况)

另一个有趣的趋势是,早年流行于男孩的名字近年来“变性了”,例如Lesley或Leslie。回到top1000数据集,找出其中以"lesl"开头的一组名字:

all_names = pd.Series(top1000.name.unique())
lesley_like = all_names[all_names.str.lower().str.contains('lesl')]
lesley_like
632     Leslie
2294    Lesley
4262    Leslee
4728     Lesli
6103     Lesly
dtype: object

然后利用这个结果过滤其他的名字,并按名字分组计算出生数以查看相对频率:

filtered = top1000[top1000.name.isin(lesley_like)]
filtered.groupby('name').births.sum()
name
Leslee      1082
Lesley     35022
Lesli        929
Leslie    370429
Lesly      10067
Name: births, dtype: int64

接下来,我们按性别和年度进行聚合,并按年度进行规范化处理:

table = filtered.pivot_table('births', index='year',columns='sex', aggfunc='sum')
table = table.div(table.sum(1), axis=0)
table
sex F M
year
1880 0.091954 0.908046
1881 0.106796 0.893204
1882 0.065693 0.934307
1883 0.053030 0.946970
1884 0.107143 0.892857
... ... ...
2006 1.000000 NaN
2007 1.000000 NaN
2008 1.000000 NaN
2009 1.000000 NaN
2010 1.000000 NaN

131 rows × 2 columns

最后,就可以轻松绘制一张分性别的年度曲线图了(如图2-10所示):

table.plot(style={'M': 'k-', 'F': 'k--'})
<matplotlib.axes._subplots.AxesSubplot at 0x7f8b339f3ac8>

USDA食品数据库

美国农业部(USDA)制作了一份有关食物营养信息的数据库。Ashley Williams制作了该数据的JSON版( http://ashleyw.co.uk/project/food-nutrient-database )。 其中的记录如下所示:

{"id": 21441,"description": "KENTUCKY FRIED CHICKEN, Fried Chicken, EXTRA CRISPY,
Wing, meat and skin with breading","tags": ["KFC"],"manufacturer": "Kentucky Fried Chicken",
"group": "Fast Foods","portions": [{"amount": 1,"unit": "wing, with skin","grams": 68.0},...],"nutrients": [{"value": 20.8,"units": "g","description": "Protein","group": "Composition"},...]
}

每种食物都带有若干标识性属性以及两个有关营养成分和分量的列表。这种形式的数据不是很适合分析工作,因此我们需要做一些规整化以使其具有更好用的形式。

从上面列举的那个网址下载并解压数据之后,你可以用任何喜欢的JSON库将其加载到Python中。我用的是Python内置的json模块:

import json
import pandas as pd# Make display smaller
pd.options.display.max_rows = 10db = json.load(open('datasets/usda_food/database.json'))
len(db)
6636

db中的每个条目都是一个含有某种食物全部数据的字典。nutrients字段是一个字典列表,其中的每个字典对应一种营养成分:

db[0].keys()
dict_keys(['id', 'description', 'tags', 'manufacturer', 'group', 'portions', 'nutrients'])
db[0]['nutrients'][0]
{'value': 25.18,'units': 'g','description': 'Protein','group': 'Composition'}
len(db[0]['nutrients'])
162
len(db[1]['nutrients'])
237
nutrients = pd.DataFrame(db[0]['nutrients'])
nutrients
description group units value
0 Protein Composition g 25.180
1 Total lipid (fat) Composition g 29.200
2 Carbohydrate, by difference Composition g 3.060
3 Ash Other g 3.280
4 Energy Energy kcal 376.000
... ... ... ... ...
157 Serine Amino Acids g 1.472
158 Cholesterol Other mg 93.000
159 Fatty acids, total saturated Other g 18.584
160 Fatty acids, total monounsaturated Other g 8.275
161 Fatty acids, total polyunsaturated Other g 0.830

162 rows × 4 columns

nutrients['id'] = db[0]['id']
nutrients
description group units value id
0 Protein Composition g 25.180 1008
1 Total lipid (fat) Composition g 29.200 1008
2 Carbohydrate, by difference Composition g 3.060 1008
3 Ash Other g 3.280 1008
4 Energy Energy kcal 376.000 1008
... ... ... ... ... ...
157 Serine Amino Acids g 1.472 1008
158 Cholesterol Other mg 93.000 1008
159 Fatty acids, total saturated Other g 18.584 1008
160 Fatty acids, total monounsaturated Other g 8.275 1008
161 Fatty acids, total polyunsaturated Other g 0.830 1008

162 rows × 5 columns

在将字典列表转换为DataFrame时,可以只抽取其中的一部分字段。这里,我们将取出食物的名称、分类、编号以及制造商等信息:

info_keys = ['description', 'group', 'id', 'manufacturer']
info = pd.DataFrame(db, columns=info_keys)
info[:5]
description group id manufacturer
0 Cheese, caraway Dairy and Egg Products 1008
1 Cheese, cheddar Dairy and Egg Products 1009
2 Cheese, edam Dairy and Egg Products 1018
3 Cheese, feta Dairy and Egg Products 1019
4 Cheese, mozzarella, part skim milk Dairy and Egg Products 1028
info.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 6636 entries, 0 to 6635
Data columns (total 4 columns):
description     6636 non-null object
group           6636 non-null object
id              6636 non-null int64
manufacturer    5195 non-null object
dtypes: int64(1), object(3)
memory usage: 207.5+ KB

通过value_counts,你可以查看食物类别的分布情况:

pd.value_counts(info.group)[:10]
Vegetables and Vegetable Products    812
Beef Products                        618
Baked Products                       496
Breakfast Cereals                    403
Fast Foods                           365
Legumes and Legume Products          365
Lamb, Veal, and Game Products        345
Sweets                               341
Pork Products                        328
Fruits and Fruit Juices              328
Name: group, dtype: int64

现在,为了对全部营养数据做一些分析,最简单的办法是将所有食物的营养成分整合到一个大表中。我们分几个步骤来实现该目的。首先,将各食物的营养成分列表转换为一个DataFrame,并添加一个表示编号的列,然后将该DataFrame添加到一个列表中。最后通过concat将这些东西连接起来就可以了:

顺利的话,nutrients的结果是:

pieces = []
for i in range(len(db)):nutrients = pd.DataFrame(db[i]['nutrients'])nutrients['id'] = db[i]['id']pieces.append(nutrients)# Concatenate everything into a single DataFrame
nutrients = pd.concat(pieces, ignore_index=True)
nutrients
description group units value id
0 Protein Composition g 25.180 1008
1 Total lipid (fat) Composition g 29.200 1008
2 Carbohydrate, by difference Composition g 3.060 1008
3 Ash Other g 3.280 1008
4 Energy Energy kcal 376.000 1008
... ... ... ... ... ...
389350 Vitamin B-12, added Vitamins mcg 0.000 43546
389351 Cholesterol Other mg 0.000 43546
389352 Fatty acids, total saturated Other g 0.072 43546
389353 Fatty acids, total monounsaturated Other g 0.028 43546
389354 Fatty acids, total polyunsaturated Other g 0.041 43546

389355 rows × 5 columns

我发现这个DataFrame中无论如何都会有一些重复项,所以直接丢弃就可以了:

nutrients.duplicated().sum()  # number of duplicates
14179
nutrients = nutrients.drop_duplicates()

由于两个DataFrame对象中都有"group"和"description",所以为了明确到底谁是谁,我们需要对它们进行重命名:

col_mapping = {'description' : 'food','group'       : 'fgroup'}info = info.rename(columns=col_mapping, copy=False)
info.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 6636 entries, 0 to 6635
Data columns (total 4 columns):
food            6636 non-null object
fgroup          6636 non-null object
id              6636 non-null int64
manufacturer    5195 non-null object
dtypes: int64(1), object(3)
memory usage: 207.5+ KB
col_mapping = {'description' : 'nutrient','group' : 'nutgroup'}nutrients = nutrients.rename(columns=col_mapping, copy=False)
nutrients
nutrient nutgroup units value id
0 Protein Composition g 25.180 1008
1 Total lipid (fat) Composition g 29.200 1008
2 Carbohydrate, by difference Composition g 3.060 1008
3 Ash Other g 3.280 1008
4 Energy Energy kcal 376.000 1008
... ... ... ... ... ...
389350 Vitamin B-12, added Vitamins mcg 0.000 43546
389351 Cholesterol Other mg 0.000 43546
389352 Fatty acids, total saturated Other g 0.072 43546
389353 Fatty acids, total monounsaturated Other g 0.028 43546
389354 Fatty acids, total polyunsaturated Other g 0.041 43546

375176 rows × 5 columns

做完这些,就可以将info跟nutrients合并起来:

ndata = pd.merge(nutrients, info, on='id', how='outer')
ndata.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 375176 entries, 0 to 375175
Data columns (total 8 columns):
nutrient        375176 non-null object
nutgroup        375176 non-null object
units           375176 non-null object
value           375176 non-null float64
id              375176 non-null int64
food            375176 non-null object
fgroup          375176 non-null object
manufacturer    293054 non-null object
dtypes: float64(1), int64(1), object(6)
memory usage: 25.8+ MB
ndata.iloc[30000]
nutrient                                       Glycine
nutgroup                                   Amino Acids
units                                                g
value                                             0.04
id                                                6158
food            Soup, tomato bisque, canned, condensed
fgroup                      Soups, Sauces, and Gravies
manufacturer
Name: 30000, dtype: object

我们现在可以根据食物分类和营养类型画出一张中位值图(如图14-11所示):

result = ndata.groupby(['nutrient', 'fgroup'])['value'].quantile(0.5)
result.head()
nutrient          fgroup
Adjusted Protein  Sweets                               12.900Vegetables and Vegetable Products     2.180
Alanine           Baby Foods                            0.085Baked Products                        0.248Beef Products                         1.550
Name: value, dtype: float64
result['Zinc, Zn'].sort_values().plot(kind='barh')
<matplotlib.axes._subplots.AxesSubplot at 0x7fcbc3b679b0>

只要稍微动一动脑子,就可以发现各营养成分最为丰富的食物是什么了:

by_nutrient = ndata.groupby(['nutgroup', 'nutrient'])get_maximum = lambda x: x.loc[x.value.idxmax()]
get_minimum = lambda x: x.loc[x.value.idxmin()]max_foods = by_nutrient.apply(get_maximum)[['value', 'food']]
max_foods
value food
nutgroup nutrient
Amino Acids Alanine 8.009 Gelatins, dry powder, unsweetened
Arginine 7.436 Seeds, sesame flour, low-fat
Aspartic acid 10.203 Soy protein isolate
Cystine 1.307 Seeds, cottonseed flour, low fat (glandless)
Glutamic acid 17.452 Soy protein isolate
... ... ... ...
Vitamins Vitamin D2 (ergocalciferol) 28.100 Mushrooms, maitake, raw
Vitamin D3 (cholecalciferol) 27.400 Fish, halibut, Greenland, raw
Vitamin E (alpha-tocopherol) 149.400 Oil, wheat germ
Vitamin E, added 46.550 Cereals ready-to-eat, GENERAL MILLS, Multi-Gra...
Vitamin K (phylloquinone) 1714.500 Spices, sage, ground

94 rows × 2 columns

# make the food a little smaller
max_foods.food = max_foods.food.str[:50]
max_foods.food
nutgroup     nutrient
Amino Acids  Alanine                                         Gelatins, dry powder, unsweetenedArginine                                             Seeds, sesame flour, low-fatAspartic acid                                                 Soy protein isolateCystine                              Seeds, cottonseed flour, low fat (glandless)Glutamic acid                                                 Soy protein isolate...
Vitamins     Vitamin D2 (ergocalciferol)                               Mushrooms, maitake, rawVitamin D3 (cholecalciferol)                        Fish, halibut, Greenland, rawVitamin E (alpha-tocopherol)                                      Oil, wheat germVitamin E, added                Cereals ready-to-eat, GENERAL MILLS, Multi-Gra...Vitamin K (phylloquinone)                                    Spices, sage, ground
Name: food, Length: 94, dtype: object

由于得到的DataFrame很大,所以不方便在书里面全部打印出来。这里只给出"Amino Acids"营养分组:

max_foods.loc['Amino Acids']['food']
nutrient
Alanine                          Gelatins, dry powder, unsweetened
Arginine                              Seeds, sesame flour, low-fat
Aspartic acid                                  Soy protein isolate
Cystine               Seeds, cottonseed flour, low fat (glandless)
Glutamic acid                                  Soy protein isolate...
Serine           Soy protein isolate, PROTEIN TECHNOLOGIES INTE...
Threonine        Soy protein isolate, PROTEIN TECHNOLOGIES INTE...
Tryptophan        Sea lion, Steller, meat with fat (Alaska Native)
Tyrosine         Soy protein isolate, PROTEIN TECHNOLOGIES INTE...
Valine           Soy protein isolate, PROTEIN TECHNOLOGIES INTE...
Name: food, Length: 19, dtype: object

2012联邦选举委员会数据库

美国联邦选举委员会发布了有关政治竞选赞助方面的数据。其中包括赞助者的姓名、职业、雇主、地址以及出资额等信息。我们对2012年美国总统大选的数据集比较感兴趣(http://www.fec.gov/disclosurep/PDownload.do) 。我在2012年6月下载的数据集是一个150MB的CSV文件(P00000001-ALL.csv),我们先用pandas.read_csv将其加载进来

import pandas as pd# Make display smaller
pd.options.display.max_rows = 10fec = pd.read_csv('datasets/fec/P00000001-ALL.csv',low_memory=False)
fec.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1001731 entries, 0 to 1001730
Data columns (total 16 columns):
cmte_id              1001731 non-null object
cand_id              1001731 non-null object
cand_nm              1001731 non-null object
contbr_nm            1001731 non-null object
contbr_city          1001712 non-null object
contbr_st            1001727 non-null object
contbr_zip           1001620 non-null object
contbr_employer      988002 non-null object
contbr_occupation    993301 non-null object
contb_receipt_amt    1001731 non-null float64
contb_receipt_dt     1001731 non-null object
receipt_desc         14166 non-null object
memo_cd              92482 non-null object
memo_text            97770 non-null object
form_tp              1001731 non-null object
file_num             1001731 non-null int64
dtypes: float64(1), int64(1), object(14)
memory usage: 122.3+ MB

该DataFrame中的记录如下所示:

fec.iloc[123456]
cmte_id             C00431445
cand_id             P80003338
cand_nm         Obama, Barack
contbr_nm         ELLMAN, IRA
contbr_city             TEMPE...
receipt_desc              NaN
memo_cd                   NaN
memo_text                 NaN
form_tp                 SA17A
file_num               772372
Name: 123456, Length: 16, dtype: object

你可能已经想出了许多办法从这些竞选赞助数据中抽取有关赞助人和赞助模式的统计信息。我将在接下来的内容中介绍几种不同的分析工作(运用到目前为止已经学到的方法)。

不难看出,该数据中没有党派信息,因此最好把它加进去。通过unique,你可以获取全部的候选人名单:

unique_cands = fec.cand_nm.unique()
unique_cands
array(['Bachmann, Michelle', 'Romney, Mitt', 'Obama, Barack',"Roemer, Charles E. 'Buddy' III", 'Pawlenty, Timothy','Johnson, Gary Earl', 'Paul, Ron', 'Santorum, Rick','Cain, Herman', 'Gingrich, Newt', 'McCotter, Thaddeus G','Huntsman, Jon', 'Perry, Rick'], dtype=object)
unique_cands[2]
'Obama, Barack'

指明党派信息的方法之一是使用字典:

parties = {'Bachmann, Michelle': 'Republican','Cain, Herman': 'Republican','Gingrich, Newt': 'Republican','Huntsman, Jon': 'Republican','Johnson, Gary Earl': 'Republican','McCotter, Thaddeus G': 'Republican','Obama, Barack': 'Democrat','Paul, Ron': 'Republican','Pawlenty, Timothy': 'Republican','Perry, Rick': 'Republican',"Roemer, Charles E. 'Buddy' III": 'Republican','Romney, Mitt': 'Republican','Santorum, Rick': 'Republican'}

现在,通过这个映射以及Series对象的map方法,你可以根据候选人姓名得到一组党派信息:

fec.cand_nm[123456:123461]
123456    Obama, Barack
123457    Obama, Barack
123458    Obama, Barack
123459    Obama, Barack
123460    Obama, Barack
Name: cand_nm, dtype: object
fec.cand_nm[123456:123461].map(parties)
123456    Democrat
123457    Democrat
123458    Democrat
123459    Democrat
123460    Democrat
Name: cand_nm, dtype: object
fec['party'] = fec.cand_nm.map(parties)
fec['party'].value_counts()
Democrat      593746
Republican    407985
Name: party, dtype: int64

这里有两个需要注意的地方。第一,该数据既包括赞助也包括退款(负的出资额):

(fec.contb_receipt_amt > 0).value_counts()
True     991475
False     10256
Name: contb_receipt_amt, dtype: int64

为了简化分析过程,我限定该数据集只能有正的出资额:

fec = fec[fec.contb_receipt_amt > 0]

由于Barack Obama和Mitt Romney是最主要的两名候选人,所以我还专门准备了一个子集,只包含针对他们两人的竞选活动的赞助信息:

fec_mrbo = fec[fec.cand_nm.isin(['Obama, Barack','Romney, Mitt'])]

根据职业和雇主统计赞助信息

基于职业的赞助信息统计是另一种经常被研究的统计任务。例如,律师们更倾向于资助民主党,而企业主则更倾向于资助共和党。你可以不相信我,自己看那些数据就知道了。首先,根据职业计算出资总额,这很简单:

fec.contbr_occupation.value_counts()[:10]
RETIRED                                   233990
INFORMATION REQUESTED                      35107
ATTORNEY                                   34286
HOMEMAKER                                  29931
PHYSICIAN                                  23432
INFORMATION REQUESTED PER BEST EFFORTS     21138
ENGINEER                                   14334
TEACHER                                    13990
CONSULTANT                                 13273
PROFESSOR                                  12555
Name: contbr_occupation, dtype: int64

不难看出,许多职业都涉及相同的基本工作类型,或者同一样东西有多种变体。下面的代码片段可以清理一些这样的数据(将一个职业信息映射到另一个)。注意,这里巧妙地利用了dict.get,它允许没有映射关系的职业也能“通过”:

occ_mapping = {'INFORMATION REQUESTED PER BEST EFFORTS' : 'NOT PROVIDED','INFORMATION REQUESTED' : 'NOT PROVIDED','INFORMATION REQUESTED (BEST EFFORTS)' : 'NOT PROVIDED','C.E.O.': 'CEO'
}# If no mapping provided, return x
f = lambda x: occ_mapping.get(x, x)
fec.contbr_occupation = fec.contbr_occupation.map(f)

我对雇主信息也进行了同样的处理:

emp_mapping = {'INFORMATION REQUESTED PER BEST EFFORTS' : 'NOT PROVIDED','INFORMATION REQUESTED' : 'NOT PROVIDED','SELF' : 'SELF-EMPLOYED','SELF EMPLOYED' : 'SELF-EMPLOYED',
}# If no mapping provided, return x
f = lambda x: emp_mapping.get(x, x)
fec.contbr_employer = fec.contbr_employer.map(f)

现在,你可以通过pivot_table根据党派和职业对数据进行聚合,然后过滤掉总出资额不足200万美元的数据:

by_occupation = fec.pivot_table('contb_receipt_amt',index='contbr_occupation',columns='party', aggfunc='sum')
over_2mm = by_occupation[by_occupation.sum(1) > 2000000]
over_2mm
party Democrat Republican
contbr_occupation
ATTORNEY 11141982.97 7.477194e+06
CEO 2074974.79 4.211041e+06
CONSULTANT 2459912.71 2.544725e+06
ENGINEER 951525.55 1.818374e+06
EXECUTIVE 1355161.05 4.138850e+06
... ... ...
PRESIDENT 1878509.95 4.720924e+06
PROFESSOR 2165071.08 2.967027e+05
REAL ESTATE 528902.09 1.625902e+06
RETIRED 25305116.38 2.356124e+07
SELF-EMPLOYED 672393.40 1.640253e+06

17 rows × 2 columns

把这些数据做成柱状图看起来会更加清楚('barh’表示水平柱状图,如图14-12所示):

over_2mm.plot(kind='barh')
<matplotlib.axes._subplots.AxesSubplot at 0x7fccfc8b0eb8>

你可能还想了解一下对Obama和Romney总出资额最高的职业和企业。为此,我们先对候选人进行分组,然后使用本章前面介绍的类似top的方法:

def get_top_amounts(group, key, n=5):totals = group.groupby(key)['contb_receipt_amt'].sum()return totals.nlargest(n)

然后根据职业和雇主进行聚合:

grouped = fec_mrbo.groupby('cand_nm')
grouped.apply(get_top_amounts, 'contbr_occupation', n=7)
cand_nm        contbr_occupation
Obama, Barack  RETIRED                  25305116.38ATTORNEY                 11141982.97INFORMATION REQUESTED     4866973.96HOMEMAKER                 4248875.80PHYSICIAN                 3735124.94...
Romney, Mitt   HOMEMAKER                 8147446.22ATTORNEY                  5364718.82PRESIDENT                 2491244.89EXECUTIVE                 2300947.03C.E.O.                    1968386.11
Name: contb_receipt_amt, Length: 14, dtype: float64
grouped.apply(get_top_amounts, 'contbr_employer', n=10)
cand_nm        contbr_employer
Obama, Barack  RETIRED                  22694358.85SELF-EMPLOYED            17080985.96NOT EMPLOYED              8586308.70INFORMATION REQUESTED     5053480.37HOMEMAKER                 2605408.54...
Romney, Mitt   CREDIT SUISSE              281150.00MORGAN STANLEY             267266.00GOLDMAN SACH & CO.         238250.00BARCLAYS CAPITAL           162750.00H.I.G. CAPITAL             139500.00
Name: contb_receipt_amt, Length: 20, dtype: float64

对出资额分组

还可以对该数据做另一种非常实用的分析:利用cut函数根据出资额的大小将数据离散化到多个面元中:

bins = np.array([0, 1, 10, 100, 1000, 10000,100000, 1000000, 10000000])
labels = pd.cut(fec_mrbo.contb_receipt_amt, bins)
labels
411         (10, 100]
412       (100, 1000]
413       (100, 1000]
414         (10, 100]
415         (10, 100]...
701381      (10, 100]
701382    (100, 1000]
701383        (1, 10]
701384      (10, 100]
701385    (100, 1000]
Name: contb_receipt_amt, Length: 694282, dtype: category
Categories (8, interval[int64]): [(0, 1] < (1, 10] < (10, 100] < (100, 1000] < (1000, 10000] < (10000, 100000] < (100000, 1000000] < (1000000, 10000000]]

现在可以根据候选人姓名以及面元标签对奥巴马和罗姆尼数据进行分组,以得到一个柱状图:

grouped = fec_mrbo.groupby(['cand_nm', labels])
grouped.size().unstack(0)
cand_nm Obama, Barack Romney, Mitt
contb_receipt_amt
(0, 1] 493.0 77.0
(1, 10] 40070.0 3681.0
(10, 100] 372280.0 31853.0
(100, 1000] 153991.0 43357.0
(1000, 10000] 22284.0 26186.0
(10000, 100000] 2.0 1.0
(100000, 1000000] 3.0 NaN
(1000000, 10000000] 4.0 NaN

从这个数据中可以看出,在小额赞助方面,Obama获得的数量比Romney多得多。你还可以对出资额求和并在面元内规格化,以便图形化显示两位候选人各种赞助额度的比例(见图14-13):

bucket_sums = grouped.contb_receipt_amt.sum().unstack(0)
normed_sums = bucket_sums.div(bucket_sums.sum(axis=1), axis=0)
normed_sums
cand_nm Obama, Barack Romney, Mitt
contb_receipt_amt
(0, 1] 0.805182 0.194818
(1, 10] 0.918767 0.081233
(10, 100] 0.910769 0.089231
(100, 1000] 0.710176 0.289824
(1000, 10000] 0.447326 0.552674
(10000, 100000] 0.823120 0.176880
(100000, 1000000] 1.000000 NaN
(1000000, 10000000] 1.000000 NaN
normed_sums[:-2].plot(kind='barh')
<matplotlib.axes._subplots.AxesSubplot at 0x7fccfb8c1a90>

我排除了两个最大的面元,因为这些不是由个人捐赠的。

还可以对该分析过程做许多的提炼和改进。比如说,可以根据赞助人的姓名和邮编对数据进行聚合,以便找出哪些人进行了多次小额捐款,哪些人又进行了一次或多次大额捐款。我强烈建议你下载这些数据并自己摸索一下。

根据州统计赞助信息

根据候选人和州对数据进行聚合是常规操作:

grouped = fec_mrbo.groupby(['cand_nm', 'contbr_st'])
totals = grouped.contb_receipt_amt.sum().unstack(0).fillna(0)
totals = totals[totals.sum(1) > 100000]
totals[:10]
cand_nm Obama, Barack Romney, Mitt
contbr_st
AK 281840.15 86204.24
AL 543123.48 527303.51
AR 359247.28 105556.00
AZ 1506476.98 1888436.23
CA 23824984.24 11237636.60
CO 2132429.49 1506714.12
CT 2068291.26 3499475.45
DC 4373538.80 1025137.50
DE 336669.14 82712.00
FL 7318178.58 8338458.81

如果对各行除以总赞助额,就会得到各候选人在各州的总赞助额比例:

percent = totals.div(totals.sum(1), axis=0)
percent[:10]
cand_nm Obama, Barack Romney, Mitt
contbr_st
AK 0.765778 0.234222
AL 0.507390 0.492610
AR 0.772902 0.227098
AZ 0.443745 0.556255
CA 0.679498 0.320502
CO 0.585970 0.414030
CT 0.371476 0.628524
DC 0.810113 0.189887
DE 0.802776 0.197224
FL 0.467417 0.532583

总结

我们已经完成了正文的最后一章。附录中有一些额外的内容,可能对你有用。

本书第一版出版已经有5年了,Python已经成为了一个流行的、广泛使用的数据分析语言。你从本书中学到的方法,在相当长的一段时间都是可用的。我希望本书介绍的工具和库对你的工作有用。

第14章 数据分析案例--Python for Data Analysis 2nd相关推荐

  1. 《利用Python进行数据分析·第2版》第14章 数据分析案例

    第 1 章 准备工作第 2 章 Python 语法基础,IPython 和 Jupyter 第 3 章 Python 的数据结构.函数和文件 第 4 章 NumPy 基础:数组和矢量计算 第 5 章 ...

  2. 数据分析---《Python for Data Analysis》学习笔记【04】

    <Python for Data Analysis>一书由Wes Mckinney所著,中文译名是<利用Python进行数据分析>.这里记录一下学习过程,其中有些方法和书中不同 ...

  3. 《利用Python进行数据分析: Python for Data Analysis 》学习随笔

    NoteBook of <Data Analysis with Python> 3.IPython基础 Tab自动补齐 变量名 变量方法 路径 解释 ?解释, ??显示函数源码 ?搜索命名 ...

  4. 第03章 Python的数据结构、函数和文件--Python for Data Analysis 2nd

    本章讨论Python的内置功能,这些功能本书会用到很多.虽然扩展库,比如pandas和Numpy,使处理大数据集很方便,但它们是和Python的内置数据处理工具一同使用的. 我们会从Python最基础 ...

  5. 第13章 Python建模库介绍--Python for Data Analysis 2nd

    本书中,我已经介绍了Python数据分析的编程基础.因为数据分析师和科学家总是在数据规整和准备上花费大量时间,这本书的重点在于掌握这些功能. 开发模型选用什么库取决于应用本身.许多统计问题可以用简单方 ...

  6. 第11章 时间序列--Python for Data Analysis 2nd

    时间序列(time series)数据是一种重要的结构化数据形式,应用于多个领域,包括金融学.经济学.生态学.神经科学.物理学等.在多个时间点观察或测量到的任何事物都可以形成一段时间序列.很多时间序列 ...

  7. 玩转Python大数据分析 《Python for Data Analysis》的读书笔记-第05页

    matplotlab matplotlib类似于matlab软件,是Python下的2维数据可视化软件,集成在了Python的IPython里了.用matplotlib画的图表具有很强的交互性,可用工 ...

  8. 玩转Python大数据分析 《Python for Data Analysis》的读书笔记-第08页

    在安装了EPD学习环境后读者需要安装Pandas,读者可以从pypi.python.org/pypi/pandas页面下在pandas安装包,(https://pypi.python.org/pack ...

  9. 利用python进行数据分析数据集_《利用Python进行数据分析》终章·数据分析案例·学习笔记(二)...

    一.第14章 数据分析案例 本书正文的最后一章,我们来看一些真实世界的数据集.对于每个数据集,我们会用之前介绍的方法,从原始数据中提取有意义的内容.展示的方法适用于其它数据集,也包括你的.本章包含了一 ...

最新文章

  1. XML的简单读取与写入
  2. MIGO相关bapi:BAPI_GOODSMVT_CREATE 移动类型314 E
  3. 常见数字IC设计、FPGA工程师面试题
  4. 铜线越长发电量越多,6千米长的铜线能让电灯泡发光吗?
  5. 面向对象方法开发的方法
  6. python中的画布控制_使按钮在画布上工作(tkinter)
  7. php 上传加水印,PHP 图片上传加水印实例
  8. Unity 生成APK 出错的解决方法
  9. 计算机硬件的基本组成(计算机组成原理)
  10. stc8a循迹小车程序编写实验:完成循迹功能,包括直角转弯,以及调头
  11. 图论及其应用 2013年期末考试 答案总结
  12. 数据处理-倾斜摄影OSGB合并根节点
  13. C语言-排序中的快速排序(简称快排)
  14. NiuMu PayPal/Stripe轮询系统 亲友转账模式同步回调支付结果
  15. PCISPH的通俗解释与简单实现
  16. 【论文】论文搜集+摘要翻译
  17. 浏览器前进后退静默刷新页面
  18. C语言 | 数组升序排列(冒泡排序法)
  19. 最高百万年薪,全国多家知名互联网/游戏公司热招 Cocos 人才丨9月岗位
  20. ionic3保存图片到本地相册

热门文章

  1. 教师计算机基础教学考核制度,ArticleView
  2. ccpd文件名转成xml_在Deepin V20/Ubuntu 20.04下安装佳能LBP2900+打印机的方法
  3. 萌新 学习python 途中一点疑惑记录IndexError: string index out of range
  4. mysql重复度高的字段_mysql中大表中重复字段的高效率查询的方法
  5. 磁盘计算机管理扩大,电脑磁盘存储空间怎么增大
  6. web自动化之元素定位手段工具
  7. python 期货交易_Python期货量化交易基础教程(1)
  8. 西门子博途v16系统要求_西门子博途扩展函数库LGF系列教程(1)-LGF_Frequency
  9. 逗号算命系统 v1.02
  10. UEFI原理与编程实践--EFI System Table中的输入输出