英文:
Add a fixed value to get cumulative sum
问题
I have a dataframe , I want to add a fixed number 5 to the first element of the dataframe, to get cumulative sum
import pandas as pd
data = {'Values': [10, 5000, 6000, 7000, 8000, 9000, 8000]}
df = pd.DataFrame(data)
Expected result like below, the first row is calculate 10 + 5 =15, second row is cumulative sum 15+5 = 20, then 20+5
英文:
I have a dataframe , I want to add a fixed number 5 to the first element of the dataframe, to get cumulative sum
import pandas as pd
data = {'Values': [10, 5000, 6000, 7000, 8000, 9000, 8000]}
df = pd.DataFrame(data)
Expected result like below, the first row is calculate 10 + 5 =15, second row is cumulative sum 15+5 = 20, then 20+5
please advise,thanks
答案1
得分: 3
使用 numpy.arange
直接进行操作:
step = 5
df['add'] = np.arange(1, len(df) + 1) * step + df['Values'].iloc[0]
或者:
step = 5
df['add'] = np.arange(step, len(df) * step + 1, step) + df['Values'].iloc[0]
输出结果:
Values add
0 10 15
1 5000 20
2 6000 25
3 7000 30
4 8000 35
5 9000 40
6 8000 45
只是为了有趣,这里还提供了一个纯粹使用 pandas 的方法(但我不建议使用它):
df['add'] = df.loc[[0], 'Values'].reindex(df.index).add(5, fill_value=0).cumsum()
英文:
Use numpy.arange
directly:
step = 5
df['add'] = np.arange(1, len(df)+1)*step+df['Values'].iloc[0]
Or:
step = 5
df['add'] = np.arange(step, len(df)*step+1, step)+df['Values'].iloc[0]
Output:
Values add
0 10 15
1 5000 20
2 6000 25
3 7000 30
4 8000 35
5 9000 40
6 8000 45
For fun, a pure pandas approach (but I wouldn't use it):
df['add'] = df.loc[[0], 'Values'].reindex(df.index).add(5, fill_value=0).cumsum()
答案2
得分: 0
另外,
import numpy as np
df['add'] = np.linspace(15, 45, 7)
英文:
Alternatively,
import numpy as np
df['add'] = np.linspace(15, 45, 7)
答案3
得分: 0
另一种可能的解决方案:
a = np.full(len(df), 5)
a[0] = df.iloc[0,0]
df['add'] = a.cumsum()
或者,
from itertools import accumulate
df['add'] = list(
accumulate(range(len(df)-1), lambda acc, x: acc + 5, initial=df.iloc[0,0]))
输出:
Values add
0 10 10
1 5000 15
2 6000 20
3 7000 25
4 8000 30
5 9000 35
6 8000 40
英文:
Another possible solution:
a = np.full(len(df), 5)
a[0] = df.iloc[0,0]
df['add'] = a.cumsum()
Alternatively,
from itertools import accumulate
df['add'] = list(
accumulate(range(len(df)-1), lambda acc, x: acc + 5, initial=df.iloc[0,0]))
Output:
Values add
0 10 10
1 5000 15
2 6000 20
3 7000 25
4 8000 30
5 9000 35
6 8000 40
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论