gpt4 book ai didi

python - 如何找到列的 cumsum?

转载 作者:行者123 更新时间:2023-12-04 04:16:57 26 4
gpt4 key购买 nike

dict={"asset":["S3","S2","E4","E1","A6","A8"],
"Rank":[1,2,3,4,5,6],"number_of_attributes":[2,1,2,2,1,1],
"number_of_cards":[1,2,2,1,2," "],"cards_plus1":[2,3,3,2,3," "]}

dframe=pd.DataFrame(dict,index=[1,2,3,4,5,6],
columns=["asset","Rank","number_of_attributes","number_of_cards","cards_plus1"])

我想对 "cards_plus1" 列进行 cumsum。我怎样才能做到这一点?列 cumsum 的输出应该是:02个5个8个1013

最佳答案

i want to start with zero instead of 2.. i want this outup : cards_plus1_cumsum 0 2 5 8 10 13

我们可以在求和前补一个零:

dframe["cumsum"] = np.pad(dframe["cards_plus1"][:-1].cumsum(), (1, 0), 'constant')

关于python - 如何找到列的 cumsum?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60563536/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com