(1)看看代理商: results=Series([x.split()[0] for x in frame.a.dropna()]#列表推导式) result.value_counts() (2)按Windows和 not windows用户对时区统计信息进行分解:
cframe=frame[frame.a.notnull()]#返回frame中a不为空的frame表格,原理见上图 operating_system=bp.where(cframe['a'].str.contains('Windows'),'Windows','Not Windows')#np.where(conditon,x,y)当condition为真时,输出x,否则输出Y,而且是按照a的顺序输出的,所以才能与后面的'tz'进行groupby() by_tz_os=cframe.groupby(['tz'],operating_system)#'tz'与operating_system一一对应,从而能进行分组 agg_counts=by_tz_os.size().unstack().fillna(0)#当某个字段只有windows用户时,not windows的值肯定缺失,所以补为0,unstack()使Series对象具有一个层次化索引(即唯一的键值对) (间接索引数组)indexer=agg_counts.sum(1).argsort()#sum(1)使每一行相加,argsort():x=np.array([1,4,3,-1,6,9]),x.argsort()输出为:[3,0,2,1,4,5],所以indexer后十行的值代表的agg_counts中的'tz'就是最频繁的字段 count_subset=agg_counts.take(indexer)[-10:] count_subset.plot(kind='barh',stacked=True)#True表示堆积 normed_subset=count_subset.div(count_subset.sum(1),axis=0)#使各行均为1