我有一个相当具体的算法,我想遵循。
基本上我有一个数据框架,如下所示:
month taken score
1 1 2 23
2 1 1 34
3 1 2 12
4 1 2 59
5 2 1 12
6 2 2 23
7 2 1 43
8 2 2 45
9 3 1 43
10 3 2 43
11 4 1 23
12 4 2 94
我想让“分数”列在“得分”栏被更改为100天,在该月底之前,持续地取==2。因此,如果在该月内的任何一天中,所发生的次数==2的分数都设置为100,则为=1。
所以我想要的结果是:
^{pr2}$我写了这个代码,我觉得应该这样做:
^{pr3}$但是,尽管没有抛出错误,但这似乎不会改变任何值。。。它也不会给我设置值到复制数据帧的错误。
有人能解释我做错什么了吗?
值没有被更新的原因是对
iloc
的赋值会更新前面的loc
调用返回的copy,因此原始值不会被触及。在下面是我如何处理这个问题。首先,定义一个函数
foo
。在现在,
^{pr2}$groupby
month
并调用foo
:显然,
apply
有它的缺点,但是我想不出一种向量化的方法来解决这个问题。在你能做到的
输出:
^{pr2}$几乎相同的速度,但“冷速”是赢家
相关问题 更多 >
编程相关推荐