将固定值相加以获得累积总和。

huangapple go评论105阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年5月30日 01:44:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76359359.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定