《Python数据分析实践Python数据分析实践 (35).pdf》由会员分享,可在线阅读,更多相关《Python数据分析实践Python数据分析实践 (35).pdf(3页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。
1、2021/11/21 下午5:145-4-2file:/C:/Users/sgl/Downloads/5-4-2.html1/3In:import pandas as pd pd.set_option(display.unicode.east_asian_width,True)#解决数据输出时列名不对齐的问题 df=pd.read_excel(豆瓣.xlsx)#读取数据 5.4.1数据预处理In:df.info()In:df.head()In:items=for str in df出版信息:item=infos=str.split(/)if len(infos)=5:item.append(i
2、nfos0)item.append(infos1)item.append(infos2)item.append(infos3)item.append(infos4)elif(len(infos)=4):item.append(infos0)item.append()item.append(infos1)item.append(infos2)item.append(infos3)else:item.append()item.append()item.append(infos0)item.append(infos1)item.append(infos2)items.append(item)In:i
3、tems:5 In:infoT=作者,译作者,出版社,出版时间,定价 dfinfo=pd.DataFrame(items,columns=infoT)dfinfo.head()In:df=df.join(dfinfo)2021/11/21 下午5:145-4-2file:/C:/Users/sgl/Downloads/5-4-2.html2/3In:df.head()In:df评价人数=df评价人数.str.replace(人评价,)In:df定价=df定价.str.extract(r(d+.d+)In:df.drop(labels=出版信息,axis=1,inplace=True)In:df
4、.drop(labels=Unnamed:0,axis=1,inplace=True)In:df.info()In:df评价人数=df评价人数.astype(float64)df定价=df定价.astype(float64)In:#存储为Excel文件 df.to_excel(douban250.xlsx)In:#抽取列 dfs=df书名,评分,评价人数,作者,译作者,出版社,出版时间,定价 5.4.2数据分析In:#了解排行榜图书的平均评分 print(排行榜平均评分,df评分.mean()In:#查看所有高于平均分的图书的信息 df.locdf评分df评分.mean(),In:#查看最受关
5、注的图书信息 df.locdf评分=df评分.max(),2021/11/21 下午5:145-4-2file:/C:/Users/sgl/Downloads/5-4-2.html3/3In:#根据评分和评分人数对排行榜数据的进行重新排序 df.sort_values(by=评分,评价人数,ascending=False).reset_index()In:#查看各个出版社各有多少上榜图书 df.groupby(出版社)书名.count().sort_values(ascending=False):10 In:#查看各个出版社出版图书的平均单价 df.groupby(出版社)定价.mean().sort_values(ascending=False)In:#查看各个出版社图书数量和平均单价 df.groupby(出版社).agg(书名:count,定价:mean).sort_values(by=书名,ascending=False)In: