3 回答

TA貢獻1848條經驗 獲得超6個贊
IIUC,嘗試:
a = np.diag(df)[None, :]
b = np.diag(df)[:, None]
c = a+b
np.fill_diagonal(c, np.diag(df))
df_out = df.div(c)
df_out
輸出:
10 25 26
10 1.000000 0.001692 0.053488
25 0.001692 1.000000 0.156010
26 0.053488 0.156010 1.000000

TA貢獻1856條經驗 獲得超11個贊
我認為這是解決方案,但您必須更改列和索引。
import pandas as pd
df = pd.DataFrame({530: [530, 1, 46],
61: [1, 61, 61],
330: [46, 61, 330]},
index = [530, 61, 330])
for i in range(len(df)):
for j in range(len(df)):
if i == j:
df.iloc[i,j] = df.iloc[i, j] / df.index[i]
else:
df.iloc[i,j] = df.iloc[i,j] / (df.index[i] + df.columns[j])
df

TA貢獻1864條經驗 獲得超6個贊
您可以將行除以列中的最大值來重現您的示例。
df1 = pd.DataFrame(
{
"column1": df['10'].divide(df['10'].max()),
"column2": df['25'].divide(df['25'].max()),
"column3": df['26'].divide(df['26'].max())
}
)
添加回答
舉報