简体   繁体   English

基于一个值添加新列

[英]Add a new column based on one value

I don't know if this is the right title for my question.我不知道这是否是我问题的正确标题。 Anyway, this is my simplified df:无论如何,这是我简化的df:

    change   gain
0      NaN    NaN
1    216.0  216.0
2    270.0  270.0
3    167.0  167.0
4    -80.0    0.0
5   -338.0    0.0
6   -155.0    0.0
7    253.0  253.0
8   -385.0    0.0
9    -30.0    0.0
10   198.0  198.0
11   318.0  318.0
12   266.0  266.0
13   142.0  142.0
14    85.0   85.0
15   199.0  199.0
16    53.0   53.0
17    91.0   91.0
18   187.0  187.0
19    64.0   64.0
20   118.0  118.0

I want to add another column called average_gain that starts at row 14 and that row value is equal to: df['gain'].head(14).mean() Which is 140.769231.我想添加另一个名为average_gain的列,它从第 14 行开始,该行值等于: df['gain'].head(14).mean()即 140.769231。 So far my desired output looks like this:到目前为止,我想要的 output 看起来像这样:

   change   gain  average_gain
0      NaN    NaN           NaN
1    216.0  216.0           NaN
2    270.0  270.0           NaN
3    167.0  167.0           NaN
4    -80.0    0.0           NaN
5   -338.0    0.0           NaN
6   -155.0    0.0           NaN
7    253.0  253.0           NaN
8   -385.0    0.0           NaN
9    -30.0    0.0           NaN
10   198.0  198.0           NaN
11   318.0  318.0           NaN
12   266.0  266.0           NaN
13   142.0  142.0           NaN
14    85.0   85.0    140.769231
15   199.0  199.0           NaN
16    53.0   53.0           NaN
17    91.0   91.0           NaN
18   187.0  187.0           NaN
19    64.0   64.0           NaN
20   118.0  118.0           NaN

Then for rows that are below row 14, I want to use this code,for example for row 15:然后对于第 14 行以下的行,我想使用此代码,例如第 15 行:

df.loc[15, 'average_gain'] = (df.loc[14, 'average_gain']*13)+df.loc[15,'gain'] It is the same formula for the rest of the rows. df.loc[15, 'average_gain'] = (df.loc[14, 'average_gain']*13)+df.loc[15,'gain']行的rest的公式相同。 So if the row number is n, the formula is:因此,如果行号为 n,则公式为:

df.loc[n, 'average_gain'] = (df.loc[n-1, 'average_gain']*13)+df.loc[n,'gain']

I tried these two ways but didn't work:我尝试了这两种方法但没有奏效:

def func(x):
    df['average_gain'].loc[n] = (df['average_gain'].loc[n - 1] * 13) + df['gain'].loc[n]
    return x

df['average_gain'].loc[15:] = (df['average_gain'].shift(1) * 13) + df['gain']

you can use:您可以使用:

df['average_gain'] = np.NaN
df['average_gain'][14] =  df['gain'].head(14).mean()
df

在此处输入图像描述

then apply your formula:然后应用你的公式:

for i in range(15, len(df)):
    df.loc[i, 'average_gain'] = df.loc[i-1, 'average_gain'] * 13 + df.loc[i,'gain']
df

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM