我有以下数据框:-
traffic_type date unique_visitors region total_views
desktop 01/04/2018 72 aug 50
mobileweb 01/04/2018 1 aug 60
total 01/04/2018 sum(mobileweb+desktop) aug 100
desktop 01/04/2018 75848907.6 world 20
mobileweb 01/04/2018 105737747.4 world 30
total 01/04/2018 sum(mobileweb+desktop) world 40
这可能是一个重复,所以任何类似问题的链接也会有帮助,我可以 在类似的行上构建脚本。 正如你所看到的,我需要在“唯一访客”栏中填写的数据是桌面和手机的总和 前提是他们在同一地区和同一日期。我需要的数据帧
traffic_type date unique_visitors region total_views
desktop 01/04/2018 72 aug 50
mobileweb 01/04/2018 1 aug 60
total 01/04/2018 73 aug 100
desktop 01/04/2018 75848907.6 world 20
mobileweb 01/04/2018 105737747.4 world 30
total 01/04/2018 181,586,655 world 40
再次,我很抱歉,如果这是重复我正在寻找参考链接,如果不是确切的解决方案
您可以使用“逐行”和“检查和求和”,如下所示
输出
对于最终答案,您应该一行一行地将这些行添加到原始数据集中
这应该能奏效。创建排除文本行的新df。然后使用
pd.to_numeric
和.groupby
区域更改为数字格式,以获得.sum()
。在此数据帧中创建一个新的“traffic type”列并设置为“total”,这样您就可以在多个列上pd.merge
将求和值返回到数据帧中。然后,使用np.where
逻辑更新值,并删除不需要的helper列以获得最终结果相关问题 更多 >
编程相关推荐