3 回答

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超6個(gè)贊
df = pd.merge(df1, df2, how='inner', right_index=True, left_index=True) # merging dataframes on date index
df['count'] = range(len(df)) # creating a column, count for easy operation
# divides dataframe in two part, one part above the not NaN row and one below
da1 = df[df['count']<=df.dropna().iloc[0]['count']]
da2 = df[df['count']>=df.dropna().iloc[0]['count']]
da1.sort_values(by=['count'],ascending=False, inplace=True)
g=[da1,da2]
num_col=len(df1.columns)
for w in range(len(g)):
list_of_col=[]
count = 0
list_of_col=[list() for i in range(len(g[w]))]
for item, rows in g[w].iterrows():
n=[]
if count==0:
for p in range(1,num_col+1):
n.append(rows[f'Loc{p}'])
else:
for p in range(1,num_col+1):
n.append(list_of_col[count-1][p-1]+ list_of_col[count-1][p-1]* rows[f'P Change_{p}'])
list_of_col[count].extend(n)
count+=1
tmp=[list() for i in range(num_col)]
for d_ in range(num_col):
for x_ in range(len(list_of_col)):
tmp[d_].append(list_of_col[x_][d_])
z1=[]
z1.extend(tmp)
for i in range(num_col):
g[w][f'Loc{i+1}']=z1[i]
da1.sort_values(by=['count'] ,inplace=True)
final_df = pd.concat([da1, da2[1:]])
calc_df = pd.DataFrame()
for i in range(num_col):
calc_df[f'Calc{i+1}']=final_df[f'Loc{i+1}']
print(calc_df)
我試圖在評(píng)論中包括我所做的所有晦澀的事情。我已經(jīng)編輯了我的代碼,讓初始數(shù)據(jù)幀不受影響。
[已編輯]:我已編輯代碼以在給定數(shù)據(jù)框中包含任意數(shù)量的列。
[已編輯:]如果 df1 和 df2 中的列名是任意的,請(qǐng)?jiān)谶\(yùn)行上層代碼之前運(yùn)行此代碼塊。我已經(jīng)使用列表理解重命名了列名!
df1.columns = [f'P Change_{i+1}' for i in range(len(df1.columns))]
df2.columns = [f'Loc{i+1}' for i in range(len(df2.columns))]

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超8個(gè)贊
也許有更好/更優(yōu)雅的方法來做到這一點(diǎn),但這對(duì)我來說效果很好:
def fill_values(df1, df2, cols1=None, cols2=None):
if cols1 is None: cols1 = df1.columns
if cols2 is None: cols2 = df2.columns
for i in reversed(range(df2.shape[0]-1)):
for col1, col2 in zip(cols1, cols2):
if np.isnan(df2[col2].iloc[i]):
val = df2[col2].iloc[i+1] + df2[col2].iloc[i+1] * df1[col1].iloc[i]
df2[col2].iloc[i] = val
return df1, df2
df1, df2 = fill_values(df1, df2)
print(df2)
Loc1 Loc2
1983-12-31 0.140160 0.136329
1984-12-31 0.169291 0.177413
1985-12-31 0.252212 0.235614
1986-12-31 0.300550 0.261526
1987-12-31 0.554444 0.261457
1988-12-31 0.544976 0.524925
1989-12-31 0.837202 0.935388
1990-12-31 0.809117 0.902741
1991-12-31 1.384158 1.544128
1992-12-31 1.745144 2.631024
1993-12-31 2.541500 3.212600
這假設(shè) df1 和 df2 中的行完全對(duì)應(yīng)(我不是查詢索引,而是查詢位置)。希望能幫助到你!

TA貢獻(xiàn)1951條經(jīng)驗(yàn) 獲得超3個(gè)贊
只是要清楚,你需要的是Loc1[year]=Loc1[next_year] + PChange[year]*Loc1[next_year]
,對(duì)吧?下面的循環(huán)將執(zhí)行您正在尋找的操作,但它只是假設(shè)兩個(gè) df 中的行數(shù)始終相等,等等(而不是匹配索引中的值)。根據(jù)您的描述,我認(rèn)為這適用于您的數(shù)據(jù)。
for i in range(df2.shape[0]-2,-1,-1): df2.Loc1[i]=df2.Loc1[i+1] + (df1.PChange_1[i]*df2.Loc1[i+1])
希望這可以幫助 :)
添加回答
舉報(bào)