python - 如何计算点击率

标签 python numpy pandas machine-learning data-analysis

这是一个例子,我有这个数据;

    datetime    keyword COUNT
0   2016-01-05  a_click 100
1   2016-01-05  a_pv    200
2   2016-01-05  b_pv    150
3   2016-01-05  b_click 90
4   2016-01-05  c_pv    120
5   2016-01-05  c_click 90

我想把它转换成这个数据

    datetime    keyword ctr
0   2016-01-05  a       0.5
1   2016-01-05  b       0.6
2   2016-01-05  c       0.75

我可以用脏代码转换数据,但我想以优雅的方式进行。

最佳答案

你可以:

df['action'] = df.keyword.str.split('_').str.get(-1)
df['keyword'] = df.keyword.str.split('_').str.get(0)
df = df.set_index(['datetime', 'keyword', 'action']).unstack().loc[:, 'COUNT']
df['ctr'] = df.click.div(df.pv)


action              click   pv   ctr
datetime   keyword                  
2016-01-05 a          100  200  0.50
           b           90  150  0.60
           c           90  120  0.75

关于python - 如何计算点击率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34602791/

相关文章:

python - 如何在 PyCharm 中运行命令而不必运行整个脚本?

python - 使用Python从包含多组数据的文件中读取指定列

python - 用 Pandas 导出数据

python - 将 pandas 操作表达为管道

Python 在 Print 语句中打印和乘以字符串

python - 坐标还原python

python - 在Python中使用自定义顺序对列表进行排序

python - 为什么矢量化版本更慢?

python - 在 Python 的 Fraction 模块中使用 numpy 向量元素

python - 加速 Django 数据库函数以对缺失值进行地理插值