gpt4 book ai didi

python - 计算 Pandas 列直到结束

转载 作者:行者123 更新时间:2023-12-04 07:39:59 25 4
gpt4 key购买 nike

import pandas
pandas.set_option('display.max_columns', None)
pandas.set_option('display.max_rows', None)
pandas.set_option('display.width', None)
df1 = pandas.read_json("""{"A":{"1062374400000":0.004270709,"1064966400000":0.010686493,"1067644800000":0.006375888,"1070236800000":0.006175395,"1072915200000":0.007879588,"1075593600000":0.01078674,"1078099200000":0.006375888,"1080777600000":0.008180328,"1083369600000":0.006576382,"1086048000000":0.006476135},"B":{"1062374400000":0.0590058044,"1064966400000":0.4393213977,"1067644800000":0.4708762019,"1070236800000":0.6544813354,"1072915200000":1.154254136,"1075593600000":null,"1078099200000":null,"1080777600000":null,"1083369600000":null,"1086048000000":null},"C":{"1062374400000":-0.0309941956,"1064966400000":0.1993213977,"1067644800000":0.0508762019,"1070236800000":0.2044813354,"1072915200000":0.514254136,"1075593600000":null,"1078099200000":null,"1080777600000":null,"1083369600000":null,"1086048000000":null},"D":{"1062374400000":null,"1064966400000":null,"1067644800000":null,"1070236800000":null,"1072915200000":null,"1075593600000":null,"1078099200000":null,"1080777600000":null,"1083369600000":null,"1086048000000":null},"E":{"1062374400000":null,"1064966400000":null,"1067644800000":null,"1070236800000":null,"1072915200000":null,"1075593600000":null,"1078099200000":null,"1080777600000":null,"1083369600000":null,"1086048000000":null}}
""")
print(df1)
我有上述算法,它转化为以下 DF:
                   A         B         C   D   E
2003-09-01 0.004271 0.059006 -0.030994 NaN NaN
2003-10-01 0.010686 0.439321 0.199321 NaN NaN
2003-11-01 0.006376 0.470876 0.050876 NaN NaN
2003-12-01 0.006175 0.654481 0.204481 NaN NaN
2004-01-01 0.007880 1.154254 0.514254 NaN NaN
2004-02-01 0.010787 NaN NaN NaN NaN
2004-03-01 0.006376 NaN NaN NaN NaN
2004-04-01 0.008180 NaN NaN NaN NaN
2004-05-01 0.006576 NaN NaN NaN NaN
2004-06-01 0.006476 NaN NaN NaN NaN
我正在尝试使用以下数学计算 D 和 E 值: Dn = E(n-1) * An En = E(n-1) + Cn + DnEn = E(n-1) + Cn + (E(n-1) * An)要使等式起作用,应手动设置 E 的第一个值。 E[0] = C[0] .
我正在使用以下算法:
df1['E'].iloc[0] = df1['C'].iloc[0] # Giving E[0] it's first value (C[0])
for i in range(1, len(df1)):
df1['D'].iloc[i] = df1['E'].iloc[i - 1] * df1['A'].iloc[i]
df1['E'].iloc[i] = df1['E'].iloc[i - 1] + df1['C'].iloc[i] + df1['D'].iloc[i]
但是,结果只计算了 D 和 E 的几行。
我想可能是因为某个时候没有 C 或 D 来计算 E,所以我也尝试了这个算法:
df1['E'].iloc[0] = df1['C'].iloc[0] # Giving E[0] it's first value (C[0])
for i in range(1, len(df1)):
df1['D'].iloc[i] = df1['E'].iloc[i - 1] * df1['A'].iloc[i]
if df1['C'].iloc[i] == numpy.nan:
df1['C'].iloc[i] = 0
if df1['D'].iloc[i] == numpy.nan:
df1['D'].iloc[i] = 0
df1['E'].iloc[i] = df1['E'].iloc[i - 1] + df1['C'].iloc[i] + df1['D'].iloc[i]
但是,结果仍然相同:
                   A         B         C         D         E
2003-09-01 0.004271 0.059006 -0.030994 NaN -0.030994
2003-10-01 0.010686 0.439321 0.199321 -0.000331 0.167996
2003-11-01 0.006376 0.470876 0.050876 0.001071 0.219943
2003-12-01 0.006175 0.654481 0.204481 0.001358 0.425783
2004-01-01 0.007880 1.154254 0.514254 0.003355 0.943392
2004-02-01 0.010787 NaN NaN 0.010176 NaN
2004-03-01 0.006376 NaN NaN NaN NaN
2004-04-01 0.008180 NaN NaN NaN NaN
2004-05-01 0.006576 NaN NaN NaN NaN
2004-06-01 0.006476 NaN NaN NaN NaN
我需要什么来确保 D 和 E 一直计算到 DF 结束?
预期结果(在评论中询问):
                    A         B          C            D          E
2003-09-01 0,00427071 0,0590058 -0,0309942 NaN -0,0309942
2003-10-01 0,0106865 0,439321 0,199321 -0,000331219 0,167996
2003-11-01 0,00637589 0,470876 0,0508762 0,00107112 0,219943
2003-12-01 0,0061754 0,654481 0,204481 0,00135824 0,425783
2004-01-01 0,00787959 1,15425 0,514254 0,00335499 0,943392
2004-02-01 0,0107867 NaN 0 0,0101761 0,953568
2004-03-01 0,00637589 NaN 0 0,00607984 0,959648
2004-04-01 0,00818033 NaN 0 0,00785024 0,967498
2004-05-01 0,00657638 NaN 0 0,00636264 0,973861
2004-06-01 0,00647614 NaN 0 0,00630685 0,980168

最佳答案

使用 fillna() 可能会稍微清洁一些设置您的所有 NaN值为 0所以你不必担心循环中的它们。应该快一点

df1.fillna(0, inplace = True)
df1['E'].iloc[0] = df1['C'].iloc[0] # Giving E[0] it's first value (C[0])
for i in range(1, len(df1)):
df1['D'].iloc[i] = df1['E'].iloc[i - 1] * df1['A'].iloc[i]
df1['E'].iloc[i] = df1['E'].iloc[i - 1] + df1['C'].iloc[i] + df1['D'].iloc[i]

A B C D E
2003-09-01 0.004271 0.059006 -0.030994 0.000000 -0.030994
2003-10-01 0.010686 0.439321 0.199321 -0.000331 0.167996
2003-11-01 0.006376 0.470876 0.050876 0.001071 0.219943
2003-12-01 0.006175 0.654481 0.204481 0.001358 0.425783
2004-01-01 0.007880 1.154254 0.514254 0.003355 0.943392
2004-02-01 0.010787 0.000000 0.000000 0.010176 0.953568
2004-03-01 0.006376 0.000000 0.000000 0.006080 0.959648
2004-04-01 0.008180 0.000000 0.000000 0.007850 0.967498
2004-05-01 0.006576 0.000000 0.000000 0.006363 0.973861
2004-06-01 0.006476 0.000000 0.000000 0.006307 0.980168

关于python - 计算 Pandas 列直到结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67537044/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com