pandas 基于其他两列的值创建列

cld4siwp  于 2023-04-18  发布在  其他
关注(0)|答案(2)|浏览(75)

这是我的dataframe:

df = pd.DataFrame({'a': [10, 20, 50], 'b': [5, 2, 20]})

这是我需要的输出:

a   b    c
0  10   5    1005
1  20   2    1009.02
2  50  20    1109.922

我想创建列c。我有一个初始值1000。c的第一行是这样计算的:

x = 1000 * (10 / 100)     # 10 is first line of column a
y = 1 + (5 / 100)         # 5 is first line of column b
z = 1000 - x

result = (x * y) + z

c的第二行是:

x = 1005 * (20 / 100)     # 20 is second line of column a
y = 1 + (2 / 100)         # 2 is second line of column b
z = 1005 - x

result = (x * y) + z

相同逻辑适用于c的最后一行。1005是来自第一行的计算结果。
我尝试了以下代码,但它不工作:

df['c'] = ((df.a / 100 * (1 + df.b / 100)).cumprod() * 1000) + 1000
hl0ma9xz

hl0ma9xz1#

由于下一个结果依赖于前一个结果,因此可以直接执行循环

init_val = 1000
for idx, row in df.iterrows():
    x = init_val * (row['a'] / 100)
    y = 1 + (row['b'] / 100)
    z = init_val - x
    init_val = (x * y) + z
    df.loc[idx, 'd'] = init_val
print(df)

    a   b         c         d
0  10   5  1005.000  1005.000
1  20   2  1009.020  1009.020
2  50  20  1109.922  1109.922
kt06eoxx

kt06eoxx2#

另一种可能的解决方案:

def calculate_c(series, prev_result=1000):
    x = prev_result * (series['a'] / 100)
    y = 1 + (series['b'] / 100)
    z = prev_result - x
    result = (x * y) + z
    return result

results = np.zeros(len(df))
prev_result = 1000

for index, row in df.iterrows():
    result = calculate_c(row, prev_result)
    prev_result = result
    results[index] = result

df['c'] = results

输出:

a   b         c
0  10   5  1005.000
1  20   2  1009.020
2  50  20  1109.922

相关问题