资源描述:
《利用python进行数据分析笔记》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、Pythonහഝړຉimportpandasaspdimportnumpyasnpimportmatplotlib.pyplotaspltimportstatsmodels.apiassmfrompandasimportDataFrame,SeriesCH02ڹ̓JSON̈́records=[json.loads(line)forlineinopen(path)]time_zone=[rec['tz']forrecinrecordsif'tz'inrec]̓ܔᦇහᓌ၄ᇇ̈́ಅํ࣐ᤩڡত۸ԅ0fromcollectionsimportdefaultdictcounts=def
2、aultdict(int)̓ڊሿེහtop10̈́fromcollectionsimportCountertoptz=Counter(time_zone)toptz.most_common(10)̓አpandasᦇහ̈́frompandasimportDataFrame,Seriesframe=DataFrame(records)tz_counts=frame['tz'].value_counts()//ᦇහѺtz_counts[:10]//top10#####fillnaڍහ๊ഘᗌ०NAclean_tz=frame['tz'].fillna('Missing')clean_tz
3、[clean_tz=='']=‘Unknown’//ဳᬯ᯾உᇙڦclean_tz==''#####Seriesጱplotڍහtz_counts[:10].plot(kind='barh',rot=0)//ଘ୵ࢶtype(tz_counts)//pandas.core.series.Series#####DataFrame݄ധnullnaframe.a.notnull()//TrueFalseclean_frame=frame[frame.a.notnull()]Өframe.a.dropna()පຎፘ̓MovieLensහഝ̈́users=pd.read_table('x
4、x/movielens/users.dat',sep='::',header=None,names=['userid','gender','age','occupation','zip’])//හഝฎአ::ړۆጱratings=pd.read_table('./Downloads/pythonහഝړຉਫդᎱ/pydata-book-master/ch02/movielens/ratings.dat',sep='::',header=None,names=['userid','movieid','rating','timestamp'])movies=pd.read_table
5、('./Downloads/pythonහഝړຉਫդᎱ/pydata-book-master/ch02/movielens/movies.dat',sep='::',header=None,names=['movieid','title','genres'])#####pandasጱmergeڍහଚusersӨratingsdata=pd.merge(pd.merge(ratings,users),movies)//cool!data.ix[0]//ᒫӞᤈ#####pandasጱpivot_tableڍහᘸѺmean_ratings=data.pivot_table('ra
6、ting',index='title',columns='gender',aggfunc='mean’)//࣐ଘጱړኪ᮱ྯڦ#####ᬦᄁᦧړහഝӧ᪃250ጱኪrating_by_title=data.groupby('title').size()//ೲኪጱᦧړහፓړᕟtitle$1,000,000Duck(1971)37'NightMother(1986)70'TilThereWasYou(1997)52active_titles=rating_by_title.index[rating_by_title>=250]//ᬦᄁmean_ratings=mean
7、_ratings.ix[active_titles]//ဳixӨindexጱ܄ڦ#####ঀᥡռ๋ࡅཻጱኪtop_female_ratings=mean_ratings.sort_index(by='F',ascending=False)#####ካঀᦧړړྏ๋य़ጱኪmean_ratings['diff']=mean_ratings['M']-mean_ratings['F']sorted_by_diff=mean_ratings.sort_index(by='diff’)//ঀๅࡅཻጱ