【问题标题】:python, pandas, autocorrelatoin by groupspython,pandas,按组关联
【发布时间】:2018-06-28 21:17:57
【问题描述】:
       fips  year        sc 
0       1001  1990 -0.707465
1       1001  1991 -0.707465
2       1001  1992 -0.707465
3       1001  1993 -0.707465
4       1001  1994 -0.707465
5       1001  1995 -0.707465
6       1002  1996 -0.707465
7       1002  1997 -0.607672
8       1002  1998 -0.607672
9       1002  1999 -0.607672
10      1002  2000 -0.607672
11      1002  2001 -0.607672
12      1002  2002 -0.607672 

我想按 fip 组计算 sc 变量的自相关。我试过代码:

test=sc_itpl.groupby('fips')['sc'].autocorr(lag=1)

我得到错误:

AttributeError:无法访问的可调用属性“autocorr” 'SeriesGroupBy' 对象,尝试使用 'apply' 方法

谢谢!

【问题讨论】:

  • test=sc_itpl.groupby('fips')['sc'].apply(lambda x: x.autocorr(lag=1)) 工作怎么样?
  • 是的,它有效。非常感谢。

标签: python pandas


【解决方案1】:

你可以使用GroupBy.apply:

test=sc_itpl.groupby('fips')['sc'].apply(lambda x: x.autocorr(lag=1))

【讨论】:

    猜你喜欢
    • 2016-11-03
    • 1970-01-01
    • 2016-11-22
    • 1970-01-01
    • 1970-01-01
    • 2017-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多