Statistics 如何为pandas中创建的窗口创建窗口和查找模式(聚合、窗口和查找模式)

Statistics 如何为pandas中创建的窗口创建窗口和查找模式(聚合、窗口和查找模式),statistics,pandas-groupby,window-functions,Statistics,Pandas Groupby,Window Functions,我有这样的数据帧(df): cpt units 36430 2 36440 5 36450 10 36430 1 36440 5 36450 10 36430 2 36440 6 36450 11 cpt units mode 36430 2 2 36440 5 5 36450 10 10 36430 1 2 36440 5 5 36450 10 10 36430 2 2 36440 6

我有这样的数据帧(df):

cpt     units
36430   2
36440   5
36450   10
36430   1
36440   5
36450   10
36430   2
36440   6
36450   11

cpt units   mode
36430   2   2
36440   5   5
36450   10  10
36430   1   2
36440   5   5
36450   10  10
36430   2   2
36440   6   5
36450   11  10
在对列(CPT)进行聚合、对列(units)进行窗口化和查找模式之后,我需要向上面的数据帧添加新的列(称为-mode)。我是说,我希望看到这样的情况:

cpt     units
36430   2
36440   5
36450   10
36430   1
36440   5
36450   10
36430   2
36440   6
36450   11

cpt units   mode
36430   2   2
36440   5   5
36450   10  10
36430   1   2
36440   5   5
36450   10  10
36430   2   2
36440   6   5
36450   11  10
我尝试了以下代码

df['mode'] = df.groupby(['cpt'])['units'].transform(lambda x: x.mode)
df['mode'] = df.groupby(['cpt'])['units'].transform(lambda x: x.mode())
from scipy.stats import mode
df['mode'] = df.groupby(['cpt'])['units'].transform(lambda x: mode(x))

没有代码是按照我的要求,虽然我非常接近我的结果。我觉得在网上冲浪时,没有直接的命令来解决我的问题。请帮我写代码。提前感谢。

期待解决方案。谢谢..期待解决方案。谢谢。期待解决方案。谢谢..期待解决方案。谢谢