<jupyter_start><jupyter_text>读取数据<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
# DAU
dau = pd.read_csv('data/section4-dau.csv', date_parser=True)
dau.head(2)
# 用户信息
user_info = pd.read_csv('data/section4-user_info.csv')
user_info.head(2)
# 合并DAU和用户信息
dau_user_info = pd.merge(dau, user_info, on=['user_id', 'app_name'])
dau_user_info.head()
dau_user_info['log_month'] = dau_user_info['log_date'].map(lambda x: x[0: 7])<jupyter_output><empty_output><jupyter_text>用户群分析<jupyter_code># 按性别统计
dau_user_info.pivot_table(index='log_month', columns='gender', values='user_id', aggfunc=len)
# 按年龄段统计
dau_user_info.pivot_table(index='log_month', columns='generation', values='user_id', aggfunc=len)
# 按性别年龄统计
dau_user_info.pivot_table(index='log_month', columns=['gender', 'generation'], values='user_id', aggfunc=len)
# 按设备统计
dau_user_info.pivot_table(index='log_month', columns='device_type', values='user_id', aggfunc=len)
# 按日期和设备类型计算用户数
dau_user_info_device_summary = dau_user_info.pivot_table(index='log_date', columns='device_type', values='user_id', aggfunc=len)
dau_user_info_device_summary.head()
# 转换日期类型
dau_user_info_device_summary.index = pd.to_datetime(dau_user_info_device_summary.index)
# 画出时间序列趋势图
import matplotlib.dates as mdates

fig, ax = plt.subplots()

dau_user_info_device_summary.plot(ax=ax)
# plt.legend(loc='center right')

# ax.xaxis.set_major_formatter(mdates.DateFormatter('%m-%d'))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14343163538873996, 'fraction_numerical': 0.004021447721179625, 'mean_word_length': 10.484615384615385, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '456135', 'license': 'MIT', 'n_tokens_mistral': 661, 'n_tokens_neox': 675, 'n_words': 78, 'path': 'ch04.ipynb', 'repo_name': 'yangjiada/data_science_from_business_applications', 'size': '1492'}