渠道归因分析
Python数据分析入门Python
问题
如何进行用户获客渠道归因分析?
答题要点
import pandas as pd
# 用户行为数据(含渠道信息)
df = pd.read_csv('user_events.csv')
df['date'] = pd.to_datetime(df['date'])
# 首次接触归因(First Touch)
first_touch = df.sort_values('date').groupby('user_id').first().reset_index()
first_touch_channel = first_touch.groupby('channel').agg({
'user_id': 'count', # 归因用户数
'revenue': 'sum' # 归因收入
}).reset_index()
# 最后接触归因(Last Touch)
last_touch = df.sort_values('date').groupby('user_id').last().reset_index()
last_touch_channel = last_touch.groupby('channel')['user_id'].count()
# 线性归因(每个接触点平均分配)
touch_count = df.groupby(['user_id', 'channel']).size().reset_index(name='touches')
user_total_touches = touch_count.groupby('user_id')['touches'].transform('sum')
touch_count['attribution_weight'] = touch_count['touches'] / user_total_touches
# 按渠道汇总
linear_attribution = touch_count.groupby('channel')['attribution_weight'].sum()
print("首次接触归因:\n", first_touch_channel)
完整解析在题库里看
核心知识点、易错点和面试官想听到什么;练过的题会进入记录,答错的进错题本。
去练这道题