背景
這是數(shù)據(jù)蛙每周交作業(yè)小組成立的第7周螟加,由于每周要統(tǒng)計(jì)作業(yè)提交情況蛀恩,并根據(jù)上兩周每位童鞋提交的作業(yè)數(shù)量、獲得的評論數(shù)和喜歡數(shù)選出本周最受歡迎的作者彻磁。恰好這周又剛剛開始學(xué)習(xí)pandas哥桥,剛好拿來練手辙浑,所以寫了這篇報(bào)告。
數(shù)據(jù)獲取
獲取方法借鑒群主的數(shù)據(jù)分析實(shí)戰(zhàn)---通過爬蟲管理社群作業(yè)拟糕,寫的十分詳細(xì)判呕,再次感謝群主大大。
1.基本思路:采取requests + BeautifulSoup + pymysql爬取數(shù)據(jù)蛙每周交作業(yè)小組截至今天上午提交的作業(yè)送滞,然后保存至數(shù)據(jù)庫中侠草。
2.爬取字段:
name:作者簡書ID
title:文章標(biāo)題
publish_time:發(fā)布文章時(shí)間
word_age:文章字?jǐn)?shù)
comments_count:該篇文章獲得的評論數(shù)
likes_count:該篇文章獲得的喜歡數(shù)
注意:群主凡人求索的文章爬取了前4個(gè)字段,要爬取評論數(shù)和喜歡數(shù)犁嗅,需要自行添加一下边涕。
數(shù)據(jù)分析部分主要借鑒了小佳童鞋的《瘋狂數(shù)據(jù)分析》第一周成員學(xué)習(xí)情況分析,感謝~
數(shù)據(jù)導(dǎo)入
在數(shù)據(jù)獲取時(shí)褂微,將數(shù)據(jù)存入了crazydata庫的表homework中功蜓,首先從數(shù)據(jù)庫中讀取交作業(yè)數(shù)據(jù):
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
import pymysql
dbconn = pymysql.connect(host='localhost',database='crazydata',user='root',password='root',port=3306,charset='utf8')
sqlcmd = "select * from homework"
data = pd.read_sql(sqlcmd, dbconn)
查看交作業(yè)的整體情況:
data.info()
可知,截至第七周一共提交了379篇文章宠蚂。
獲取數(shù)據(jù)樣本:
data.head()
data['name'].nunique()
結(jié)果為69式撼。
綜上截至第七周(2019年1月27日上午6時(shí))一共有69位小組成員提交了共379篇文章。
數(shù)據(jù)清洗
1.發(fā)布時(shí)間
from dateutil.parser import parse
data.publish_time = data.publish_time.str.strip('*') #清除發(fā)布時(shí)間里面多余的*
for i in data.publish_time:
i = parse(i)
data.publish_time = pd.to_datetime(data.publish_time) #將發(fā)布時(shí)間轉(zhuǎn)化為datetime類型
data['date'] = data.publish_time.dt.day
data['hour'] = data.publish_time.dt.hour
2.文章字?jǐn)?shù)
data.word_age = data.word_age.str.strip('字?jǐn)?shù)') #刪除字?jǐn)?shù)字段多余的“字?jǐn)?shù)”
data.word_age = data.word_age.astype('int') #將類型轉(zhuǎn)換為int類型
3.文章獲取的評論數(shù)
data.comments_count = data.comments_count.str.strip('\n') # 刪除評論數(shù)字段多余的\n
data.comments_count = data.comments_count.astype('int')
4.文章獲取的喜歡數(shù)
data.likes_count = data.likes_count.astype('int')
再次查看數(shù)據(jù)整體情況及樣本:
data.info()
data.head()
此時(shí)求厕,發(fā)布時(shí)間字段已經(jīng)轉(zhuǎn)為datetime類型著隆,文章字?jǐn)?shù)、評論數(shù)和喜歡數(shù)字段已轉(zhuǎn)為int類型甘改。
數(shù)據(jù)分析
文章發(fā)布篇數(shù)
data.groupby(['name'])[['title']].count().describe()
data.groupby(['name'])['title'].count().sort_values(ascending = False).head(5)
發(fā)布文章數(shù)最多的前五名分別是:1點(diǎn)點(diǎn)De小任性丶 腾节、yimengtianya1忘嫉、Great_smile、夜希辰案腺、凡人求索 庆冕。
提交作業(yè)時(shí)間
data['dayofweek'] = data['publish_time'].dt.dayofweek # 獲取提交作業(yè)的時(shí)間是一周中的星期幾
data.groupby(['dayofweek'])['name'].count().plot(kind='bar')
plt.show()
data.groupby(['hour'])['name'].count().plot(kind='bar')
plt.show()
大部分童鞋在周日提交作業(yè)。
一天中提交作業(yè)的時(shí)段集中在中午和晚上劈榨。晚上7點(diǎn)左右提交的數(shù)量最多访递。
文章字?jǐn)?shù)、獲得評論數(shù)同辣、喜歡數(shù)
data.groupby(['name'])[['word_age', 'comments_count', 'likes_count']].sum().describe()
1.文章字?jǐn)?shù)
查看上圖,發(fā)現(xiàn)每位作者寫的總文章字?jǐn)?shù)竟然有0個(gè)字的响巢,這不科學(xué)描滔,篩選出總文章字?jǐn)?shù)為0的作者及其提交的文章:
df = data.groupby(['name'])[['word_age']].sum()
df[ df.word_age==0 ]
data[data['name'] == 'EricGuoxm' ]
剔除該篇文章重新統(tǒng)計(jì)文章字?jǐn)?shù)拘泞、獲得評論數(shù)、獲得喜歡數(shù):
data.drop(286, inplace=True)
data.groupby(['name'])[['word_age', 'comments_count', 'likes_count']].sum().describe()
截至第七周,68位小組成員平均每位寫了4078個(gè)字铡原,最多的寫了16178個(gè)字偷厦。
2.文章獲得評論數(shù)
查看獲取的評論數(shù)最多的前5名小組成員:
data.groupby(['name'])[['comments_count']].sum().sort_values(by='comments_count', ascending=False).head(5)
查看獲得評論數(shù)最多的單篇文章及其作者的前5名:
data[['name', 'title', 'comments_count']].sort_values('comments_count', ascending=False)
平均每位作者獲得了2個(gè)評論,文章的總評論數(shù)和單篇文章獲得評論數(shù)最多的均為凡人求索燕刻。
3.文章喜歡數(shù)
查看獲取的喜歡數(shù)最多的前5名小組成員:
data.groupby(['name'])[['likes_count']].sum().sort_values(by='likes_count', ascending=False).head(5)
查看獲得喜歡數(shù)最多的單篇文章及其作者的前5名:
data[['name', 'title', 'likes_count']].sort_values('likes_count', ascending=False)
estate47童鞋在獲得的喜歡數(shù)總數(shù)和獲得喜歡數(shù)最多的單篇文章均居榜首只泼,群主凡人求索兩項(xiàng)均榜上有名。平均每位童鞋獲得的喜歡數(shù)為12個(gè)卵洗,我們的文章質(zhì)量要繼續(xù)提升啊请唱。
哪篇文章是截至本周最受歡迎的文章?
我們采取 受歡迎度=文章獲得評論數(shù)*0.2+文章獲得喜歡數(shù)*0.8 的算法:
data['popular_degree'] = 0.2*data['comments_count'] + 0.8*data['likes_count']
data[['name', 'title', 'popular_degree']].sort_values(by='popular_degree', ascending=False)
截至本周最受歡迎的前5篇文章如下:誰是截至本周最受歡迎的作者过蹂?
采用 受歡迎度=提交文章總數(shù)*0.2+文章獲得總評論數(shù)*0.2+文章獲得總喜歡數(shù)*0.6 的算法:
popular_author = data.groupby(['name'], as_index=False)[ [ 'title', 'comments_count', 'likes_count'] ].agg({'title': 'count', 'comments_count': 'sum', 'likes_count': 'sum'})
popular_author['popular_degree'] = popular_author['title'] * 0.2 + popular_author['comments_count'] * 0.2 + popular_author['likes_count'] * 0.6
popular_author.sort_values(by='popular_degree', ascending=False).head(10)
截至本周最受歡迎的作者如下:總結(jié):
1.截至2019.1.27上午6時(shí)十绑,一共有69位小組成員提交了共379篇文章。
2.一周中提交作業(yè)的時(shí)間集中在每周日酷勺,一天中提交作業(yè)的時(shí)段集中在中午11-12時(shí)和晚上17-24時(shí)本橙。
3.平均每位小組成員發(fā)布了5.5篇文章。最多的已發(fā)布19篇脆诉。
4.平均寫作字?jǐn)?shù)4078個(gè)甚亭。最多的累積寫作字?jǐn)?shù)達(dá)到16178個(gè)。
5.最受歡迎的單篇文章獲得了95個(gè)喜歡击胜,最受歡迎的作者獲得了116個(gè)喜歡亏狰。