执行pct_change()只考虑多个ID的时间序列数据帧中的前一年?

时间:2017-12-24 12:25:27

标签: python pandas dataframe

我有一个示例数据框" df:

df = pd.DataFrame({'Year': [2000, 2002, 2003, 2004] + [1998, 1999, 2003, 2004],
                   'Name': ['A'] * 4 + ['B'] * 4,
                   'Value': [4, 1, 1, 3] + [34, 23, 22, 11]})

我如何使用groupby" Name"并使用函数" pct_change()"这个函数只考虑我的时间序列中的前一年?即该函数应在2002年返回N / A,名称为" A" (自2001年失踪)和2003年的名字" B" (自2002年失踪)?

1 个答案:

答案 0 :(得分:0)

您应该可以使用Database.database().reference().child("likes").child(self.userUID).observe(.value, with: { (snapshot) in if let dictionary = snapshot.value as? [String: Any]{ let uid = dictionary[""] as! String print("uid") } }) + groupby -

执行此操作
apply
def f(x):
    r = np.arange(x.index.min(), x.index.max() + 1)
    return x.reindex(r).pct_change(fill_method=None)

df = df.set_index('Year')\
       .groupby('Name')\
       .Value.apply(f)\
       .reset_index() 
相关问题