探索一下Pandas的累加函数cumsum,我们可以先建立一个空的dataframe,用于存放接下来的值。
import pandas as pd columns = ['id_','name','money'] data_frame = pd.DataFrame(columns = columns) #创建一个3列的空dataframe
1. 赋值:给每一列增加数据
#建立数据 id_ = [1, 3, 2, 3, 2] name = ['A','B','C','D','E'] money = [100, 400, 280, 170, 500] data_frame['id_'] = id_ data_frame['name'] = name data_frame['money'] = money #给dataframe赋值
print(' 1. did not cumsum is: '+str(data_frame))
2. 直接对 'money' 列进行cumsum:
data_frame['cumsum_money'] = data_frame['money'].cumsum() print(' 2. the cumsum money is: '+str(data_frame))
3. 按照 'id_' 列的分组 group by,再进行分别cumsum,如图所示:
# 先按照id分组,再对money列进行cumsum data_2 = data_frame.groupby(['id_']) print(data_2) data_frame['cumsum_money_groupby'] = data_2['money'].cumsum() print(' 3. the cumsum money after groupby is: '+str(data_frame))
##