針對douyu_60937 直播間 (2018/11/19 19:04:18 - 2018/11/20 7:56:42) 這個時間段的數據分析python
以上數據是從直播間的彈幕中提取的相關數據,每一個字段解釋爲json
{ 'id': '惟一標識', 'user': '用戶名', 'cont': '發送的信息', 'level': '用戶等級', 'sign': '牌子', 'sign_leve': '牌子等級', 'rid': '發言房間號', }
pandas==0.23.4 matplotlib==3.0.2 numpy==1.15.4 datetime
import pandas as pd import matplotlib.pyplot as plt import numpy as np import datetime path = "douyu_60937.xlsx" data = pd.read_excel(path)
t = data[['user', 'sign', 'sign_leve']].drop_duplicates(subset=['user']) # 刪除重複用戶 t = data.groupby('sign').sign_leve.agg(['mean', 'min', 'max']) t.sort_values(['max'], ascending=False, inplace=True) print(t) print(t[:20].to_dict())
{'mean': {'小人蔘': 11.848837209302326, '196': 16.475254730713246, '女流': 11.418693982074263, 'Amss': 10.0, '水煮肉': 26.0, '小殭屍': 9.93750495049505, '339': 18.941176470588236, '金髮雅': 24.0, '猛男': 7.768361581920904, '小豆包': 12.676724137931034, '阿冷': 10.416666666666666, '寅子': 7.059241706161138, '小癢蟲': 10.718562874251496, 'S1un': 22.0, '小緑帽': 9.938271604938272, '集團軍': 7.909323116219668, '小烏賊': 8.377464788732395, '點子王': 5.686131386861314, '小肚皮': 7.034911587538053, '林Q': 21.0}, 'min': {'小人蔘': 2, '196': 4, '女流': 3, 'Amss': 4, '水煮肉': 26, '小殭屍': 1, '339': 12, '金髮雅': 24, '猛男': 1, '小豆包': 3, '阿冷': 6, '寅子': 1, '小癢蟲': 2, 'S1un': 22, '小緑帽': 1, '集團軍': 1, '小烏賊': 1, '點子王': 3, '小肚皮': 1, '林Q': 21}, 'max': {'小人蔘': 30, '196': 30, '女流': 30, 'Amss': 27, '水煮肉': 26, '小殭屍': 25, '339': 25, '金髮雅': 24, '猛男': 23, '小豆包': 23, '阿冷': 22, '寅子': 22, '小癢蟲': 22, 'S1un': 22, '小緑帽': 22, '集團軍': 22, '小烏賊': 22, '點子王': 22, '小肚皮': 21, '林Q': 21}}
sign_max_count = data[['user', 'sign', 'sign_leve']].drop_duplicates(subset=['user']) # 刪除重複用戶 sign_max_count = data[['sign']].groupby(['sign']).agg({'sign': 'count'}).rename( columns={'sign': 'sign_count'}) sign_max_count.sort_values(['sign_count'], ascending=False, inplace=True) print(sign_max_count) print(sign_max_count[:20].to_dict())
{'sign_count': {'小肚皮': 15439, '小殭屍': 12625, '集團軍': 1566, '196': 1374, '女流': 781, '影魔王': 640, '大馬猴': 429, '寅子': 422, '小緑帽': 405, '小8路': 397, '小烈驢': 370, '小烏賊': 355, '小贏僧': 355, '保安團': 343, '豬芳芳': 243, '小豆包': 232, '王菠蘿': 187, '二帆': 184, '猛男': 177, '汽車人': 173}}
now_data = data[['user', 'level']].drop_duplicates(subset=['user']) # 刪除重複用戶 now_data = data[['level']].groupby(['level']).agg({'level': 'count'}).rename( columns={'level': 'level_count'}) now_data.sort_values(['level_count'], ascending=False, inplace=True)
{'level_count': {16: 3159, 19: 3122, 17: 3086, 21: 2984, 18: 2882, 15: 2832, 22: 2624, 23: 2564, 20: 2545, 13: 2379, 14: 2308, 24: 2274, 11: 2066, 12: 1894, 7: 1781, 9: 1753, 10: 1690, 8: 1678, 5: 1645, 25: 1554}}
df = data df = df.loc[:, ('cont', 'uptime')] df = df.set_index('uptime') result = df.resample('H').count().rename(columns={'cont': 'user_count'}) print(result) result = result.to_dict() s = {} for k, v in result['user_count'].items(): s[k.to_pydatetime()] = v result['user_count'] = s print(result) return result
{'user_count': {datetime.datetime(2018, 11, 19, 19, 0): 12707, datetime.datetime(2018, 11, 19, 20, 0): 12374, datetime.datetime(2018, 11, 19, 21, 0): 19340, datetime.datetime(2018, 11, 19, 22, 0): 13530, datetime.datetime(2018, 11, 19, 23, 0): 8, datetime.datetime(2018, 11, 20, 0, 0): 2, datetime.datetime(2018, 11, 20, 1, 0): 1, datetime.datetime(2018, 11, 20, 2, 0): 0, datetime.datetime(2018, 11, 20, 3, 0): 0, datetime.datetime(2018, 11, 20, 4, 0): 5, datetime.datetime(2018, 11, 20, 5, 0): 1, datetime.datetime(2018, 11, 20, 6, 0): 11, datetime.datetime(2018, 11, 20, 7, 0): 23}}
df = data.drop_duplicates(subset=['user']) df = df.loc[:, ('user', 'uptime')] df = df.set_index('uptime') result = df.resample('H').count().rename(columns={'user': 'user_count'}) print(result) result = result.to_dict() s = {} for k, v in result['user_count'].items(): s[k.to_pydatetime()] = v result['user_count'] = s print(result)
{'user_count': {datetime.datetime(2018, 11, 19, 19, 0): 4223, datetime.datetime(2018, 11, 19, 20, 0): 2207, datetime.datetime(2018, 11, 19, 21, 0): 3843, datetime.datetime(2018, 11, 19, 22, 0): 1875, datetime.datetime(2018, 11, 19, 23, 0): 6, datetime.datetime(2018, 11, 20, 0, 0): 0, datetime.datetime(2018, 11, 20, 1, 0): 1, datetime.datetime(2018, 11, 20, 2, 0): 0, datetime.datetime(2018, 11, 20, 3, 0): 0, datetime.datetime(2018, 11, 20, 4, 0): 1, datetime.datetime(2018, 11, 20, 5, 0): 1, datetime.datetime(2018, 11, 20, 6, 0): 1, datetime.datetime(2018, 11, 20, 7, 0): 4}}
def autolabel(ax, rects, xpos='center'): xpos = xpos.lower() ha = {'center': 'center', 'right': 'left', 'left': 'right'} offset = {'center': 0.5, 'right': 0.57, 'left': 0.43} for rect in rects: height = rect.get_height() ax.text(rect.get_x() + rect.get_width() * offset[xpos], 1.01 * height, '{}'.format(height), ha=ha[xpos], va='bottom') def sign_bar_wiht_leve(t1): """ 粉絲牌等級狀況柱狀圖 :return: """ mean = [round(x, 1) for x in t1['mean'].values()] min = t1['min'].values() max = t1['max'].values() ind = np.arange(len(mean)) width = 0.35 fig, ax = plt.subplots() rects_2 = ax.bar(ind, min, width / 2, color='IndianRed', label='最低等級') rects_1 = ax.bar(ind + width / 2, mean, width / 2, color='SkyBlue', label='平均等級') rects_3 = ax.bar(ind + width, max, width / 2, color='Black', label='最高等級') ax.set_ylabel('等級') ax.set_title('粉絲牌等級 (2018/11/19 19:04:18 - 2018/11/20 7:56:42)') ax.set_xticks(ind) ax.set_xticklabels(t1['mean'].keys()) ax.legend() fig = plt.gcf() fig.set_size_inches(30, 10.5) autolabel(ax=ax, rects=rects_1, xpos="center") autolabel(ax=ax, rects=rects_2, xpos="center") autolabel(ax=ax, rects=rects_3, xpos="center") fig.savefig("粉絲牌等級.jpg") fig.show()
def sign_pie_with_count(t2): """ 粉絲牌佔比 :param t2: :return: """ sizes = list(t2['sign_count'].values())[:5] labels = list(t2['sign_count'].keys())[:5] plt.figure(figsize=(8, 4)) # 調節圖形大小 explode = (0, 0, 0, 0) # 將某一塊分割出來,值越大分割出的間隙越大 patches, text1, text2 = plt.pie(sizes, labels=labels, autopct='%3.2f%%', # 數值保留固定小數位 shadow=False, # 無陰影設置 startangle=90, # 逆時針起始角度設置 pctdistance=0.8) # 數值距圓心半徑倍數距離 plt.axis('equal') plt.legend() plt.title('粉絲牌佔比 \n(2018/11/19 19:04:18 - 2018/11/20 7:56:42)') plt.savefig("粉絲牌佔比.jpg") plt.show()
def user_line_with_count(t4): """ 用戶等級分佈折線圖 :param t4: :return: """ t4 = sorted(t4['level_count'].items(), key=lambda d: d[0]) x = [i[0] for i in t4] y = [i[1] for i in t4] plt.figure(figsize=(8, 4)) plt.plot(x, y, "b--", linewidth=1) # 設置數字標籤 for a, b in zip(x, y): plt.text(a, b, b, ha='center', va='bottom', fontsize=10) plt.xlabel("用戶等級") plt.ylabel("數量") plt.title("用戶等級狀況\n(2018/11/19 19:04:18 - 2018/11/20 7:56:42)") plt.savefig("用戶等級狀況.jpg") plt.show()
def show_label(x, y, plt): # 設置數字標籤 for a, b in zip(x, y): plt.text(a, b, b, ha='center', va='bottom', fontsize=10) def user_time(t5, t6): """ 每一個時間段用戶以及彈幕量 :param t5: :param t6: :return: """ t_5 = {k.strftime("%Y-%m-%d %H"): v for k, v in t5['user_count'].items()} t_6 = {k.strftime("%Y-%m-%d %H"): v for k, v in t6['user_count'].items()} x_1 = t_5.keys() y_1 = t_5.values() x_2 = t_6.keys() y_2 = t_6.values() plt.figure(figsize=(9, 5)) plt.plot(x_1, y_1, "o-", linewidth=1, label='彈幕數量') plt.plot(x_2, y_2, "g--", linewidth=1, label='在線人數') show_label(x_1, y_1, plt) show_label(x_2, y_2, plt) plt.xticks(rotation=30) plt.legend() plt.xlabel("小時") plt.ylabel("人數") plt.title("每一個時間段用戶以及彈幕量\n(2018/11/19 19:04:18 - 2018/11/20 7:56:42)") plt.savefig("每一個時間段用戶以及彈幕量.jpg") plt.show()