假设你有这个数据框:
emails/names A B C D
0 1@ 0.20 0.30 0.40 0.6
1 2@ 0.15 0.20 0.20 0.5
2 3@ 0.10 0.05 0.03 0.2
然后:
df = df.set_index("emails/names")
numpy_df = df.to_numpy()
forbidden_rows, forbidden_cols = [], []
while len(forbidden_rows) != len(df):
row, col = np.unravel_index(numpy_df.argmin(), df.shape)
numpy_df[:, col] = np.inf
numpy_df[row, :] = np.inf
forbidden_rows.append(df.index[row])
forbidden_cols.append(df.columns[col])
for r, c in zip(forbidden_rows, forbidden_cols):
print(r, c)
打印:
3@ C
2@ A
1@ B
编辑:首先将数据帧转换为numpy.ndarray。
编辑:打印未分配的电子邮件:
对于这个数据框:
emails/names A B C D
0 1@ 0.20 0.30 0.40 0.6
1 2@ 0.15 0.20 0.20 0.5
2 3@ 0.10 0.05 0.03 0.2
3 4@ 0.10 0.05 0.03 0.2
4 5@ 0.11 0.25 0.43 0.2
5 6@ 0.12 0.35 0.53 0.3
这个:
df = df.set_index("emails/names")
numpy_df = df.to_numpy()
forbidden_rows, forbidden_cols = [], []
while len(forbidden_rows) != len(df) and len(forbidden_cols) != len(df.columns):
row, col = np.unravel_index(numpy_df.argmin(), df.shape)
numpy_df[:, col] = np.inf
numpy_df[row, :] = np.inf
forbidden_rows.append(df.index[row])
forbidden_cols.append(df.columns[col])
for r, c in zip(forbidden_rows, forbidden_cols):
print(r, c)
print("Unassigned emails:")
print(df.index[~df.index.isin(forbidden_rows)].values)
打印:
3@ C
4@ B
5@ A
6@ D
Unassigned emails:
['1@' '2@']