<jupyter_start><jupyter_text>数据处理<jupyter_code># 导入相关库
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
# 读取csv数据
dau = pd.read_csv('data/section3-dau.csv')
dpu = pd.read_csv('data/section3-dpu.csv')
install = pd.read_csv('data/section3-install.csv')
# 查看数据
dau.head()
dpu.head()
install.head()
# 合并DAU和install数据
dau_install = pd.merge(dau, install, on=['user_id', 'app_name'])

# 合并上述数据和DPU数据
dau_install_payment = pd.merge(dau_install, dpu, how='left', on=['log_date', 'app_name', 'user_id'])
dau_install.head()
dau_install_payment.head()
# 查看payment不为空数据
dau_install_payment[dau_install_payment['payment'].notnull()].head()
# 将未消费用户的消费金额设置为零
dau_install_payment['payment'] = dau_install_payment['payment'].fillna(0)
dau_install_payment.head()
# 增加两列表示月份
dau_install_payment['log_month'] = dau_install_payment['log_date'].map(lambda x: x[0: 7])
dau_install_payment['install_month'] = dau_install_payment['install_date'].map(lambda x: x[0: 7])
# 按使用月份、用户ID和首次使用月份分组汇总消费金额合计
mau_payment = dau_install_payment.groupby(['log_month', 'user_id', 'install_month'], as_index=False).agg({'payment': 'sum'})
mau_payment.head()
# 增加识别新用户和已有用户
mau_payment['user_type'] = np.where(mau_payment['log_month'] == mau_payment['install_month'], 'install', 'existing')
mau_payment.head()<jupyter_output><empty_output><jupyter_text>统计分析<jupyter_code># 按月份和用户类型分组求消费金额合计
mau_paymnet_summary = mau_payment.groupby(['log_month', 'user_type'], as_index=False).agg({'payment': 'sum'})
mau_paymnet_summary
# 按使用月份和用户类型分组汇总消费金额绘制叠加柱状图
mau_payment.pivot_table(index='log_month', columns='user_type', values='payment',
                        aggfunc='sum').plot(kind='bar', stacked=True, alpha=0.8, rot=True)

plt.ylabel('total_payment(JPY)')
plt.legend(title='user_type', loc='center right')
plt.show()
from matplotlib.pyplot import MultipleLocator
# 对比不同月份消费金额分布
install_payment = mau_payment[(mau_payment['payment'] > 0) & (mau_payment['user_type'] == 'install')]
data1 = install_payment[install_payment['log_month'] == '2013-06']['payment']
data2 = install_payment[install_payment['log_month'] == '2013-07']['payment']

bins = np.arange(0, 18000, 2000) # 设置分割的宽度，如果设成1000那么出来的图分布会不一样

plt.hist([data1.values, data2.values], bins=bins, label=['2013-06', '2013-07'])
plt.legend(title='log_month', loc='center right', )
plt.xlabel('payment (JPY)')
plt.ylabel('count')

ax = plt.gca()
ax.xaxis.set_major_locator(MultipleLocator(5000)) # 设置x轴间隔
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16813450760608487, 'fraction_numerical': 0.022818254603682947, 'mean_word_length': 8.916666666666666, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 10, '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': '623609', 'license': 'MIT', 'n_tokens_mistral': 1119, 'n_tokens_neox': 1118, 'n_words': 132, 'path': 'ch03.ipynb', 'repo_name': 'yangjiada/data_science_from_business_applications', 'size': '2498'}