任何人都可以告訴我如何更有效地使用熊貓,目前我正在做以下找出兩個項目的相關性,但這不是很快。填充蟒蛇數組沒有雙循環
for i in range(0, df.shape[0]):
for j in range(0, df.shape[0]):
if i<j:
## get the weights
wgt_i = dataWgt_df.ix[df.index[i]][0]
wgt_j = dataWgt_df.ix[df.index[j]][0]
## get the std's
std_i = dataSTD_df.loc[date][df.index[i]][0]
std_j = dataSTD_df.loc[date][df.index[j]][0]
## get the corvariance
#print(cor.ix[df.index[i]][df.index[j]])
cor = corr.ix[df.index[i]][df.index[j]]
## create running total
totalBottom = totalBottom + (wgt_i * wgt_j * std_i * std_j)
totalTop = totalTop + (wgt_i * wgt_j * std_i * std_j * cor)
我想要做的就是創建一個單位矩陣這樣
0 1 1 1 1
0 0 1 1 1
0 0 0 1 1
0 0 0 0 1
0 0 0 0 0
,我就可以用它來繁衍在各種dataframes,wgt_i wgt_j std_i std_j這將爲頂部的數據幀和然後我可以使用求和函數求和得到結果。
這裏我的主要問題是如何快速創建標識數據框,然後創建wgt_i等數據框,因爲其餘部分是相對直接的。
的身份矩陣包含1個主對角線和0其他任何地方。 –
我不是線性代數的專家,但我可以告訴它絕對不是一個單位矩陣... – Ian