首页 > 解决方案 > How can I add previous column values to to get new value in Excel?

问题描述

I am working on graph and in need data in below format. I have data in COL A. I need to calculate COL B values as in below picture.

What is the formula for obtaining this in excel?

enter image description here

标签: excelxlsxwriter

解决方案


您可以使用cumsumand shift

# sample data
df = pd.DataFrame({'COL A': np.arange(11)})

df['COL B'] = df['COL A'].shift(fill_value=0).cumsum()

输出:

    COL A  COL B
0       0      0
1       1      0
2       2      1
3       3      3
4       4      6
5       5     10
6       6     15
7       7     21
8       8     28
9       9     36
10     10     45

推荐阅读