累积总和DataFrame(.groupby())

时间:2019-12-02 20:20:00

标签: python pandas numpy dataframe pandas-groupby

我试图获得这些年来“计数器”的累计金额。基本上,在保持多索引结构的同时,将各年的总和相加。非常感谢您的帮助! :)

df = pd.DataFrame(df_raw[['counter']])
df['listings_per_zip'] = df.groupby(level=[0,1]).sum()
df = df.mean(level=[0,1])

Curren Input (df_raw)

Current Output (df)

(示例)预期输出:多年来的ZIP总和。例如,2018“ listings_per_zip”应为2008 + 2009 + ... + 2017

1 个答案:

答案 0 :(得分:1)

问题由以下代码解决:

id  post_type   passed
2   course-topic    1
4   quiz            0
6   topic           1
3   quiz         (null)
7   course-topic (null)
8   topic        (null)