In the following empty DataFrame, which is a square, symmetric matrix,
df = pd.DataFrame(np.zeros((6,6)), index=names, columns=names)
df
looks like
MCD VZ JPM PG WBA NKE
MCD 0.0 0.0 0.0 0.0 0.0 0.0
VZ 0.0 0.0 0.0 0.0 0.0 0.0
JPM 0.0 0.0 0.0 0.0 0.0 0.0
PG 0.0 0.0 0.0 0.0 0.0 0.0
WBA 0.0 0.0 0.0 0.0 0.0 0.0
NKE 0.0 0.0 0.0 0.0 0.0 0.0
how to receive the corresponding elements from the last column of a different DataFrame, shown below, df2.mi
, according to pair-names like ('MCD','VZ')
, as well as place the same values for (i,j)
in those for (j,i)
(because of matrix symmetry)? For example, 0.263357
should appear in df
for the two places ('MCD','VZ')
and ('VZ','MCD')
. A toy answer with only 3 names would be fine.
Alternatively, is there a common algorithm for populating a square, symmetric matrix by for
loop, according to the (i,j)
rules described?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…