为传输问题创建基于行的字典

Creating a dictionary based on rows for a transport problem

所以我有以下数据:

我已经使用 read_excel() 将其读入 python 并进行了一些转换,得到了以下 pandas 数据框:

现在我想制作一个字典,其中每行中的所有对 returns 该行的“Pr 单位成本”作为值,如下所示:

{('Macon', 'Tacoma'): 35.5,
    ('Macon', 'San Diego'): 35.5,
    ('Macon', 'Dallas'): 35.5,
    ('Macon', 'Denver'): 35.5,
    ('Macon', 'St. Louis'): 35.5,
    ('Macon', 'Tampa'): 35.5,
    ('Macon', 'Baltimore'): 35.5,
    ('Louisville', 'Tacoma'): 37.5,
    ('Louisville', 'San Diego'): 37.5,
    ('Louisville', 'Dallas'): 37.5,
    ('Louisville', 'Denver'): 37.5,
    ('Louisville', 'St. Louis'): 37.5,
    ('Louisville', 'Tampa'): 37.5,
    ('Louisville', 'Baltimore'): 37.5,
    ('Detroit', 'Tacoma'): 39,
    ('Detroit', 'San Diego'): 39,
    ('Detroit', 'Dallas'): 39,
    ('Detroit', 'Denver'): 39,
    ('Detroit', 'St. Louis'): 39,
    ('Detroit', 'Tampa'): 39,
    ('Detroit', 'Baltimore'): 39,
    ('Phoenix', 'Tacoma'): 36.25,
    ('Phoenix', 'San Diego'): 36.25,
    ('Phoenix', 'Dallas'): 36.25,
    ('Phoenix', 'Denver'): 36.25,
    ('Phoenix', 'St. Louis'): 36.25,
    ('Phoenix', 'Tampa'): 36.25,
    ('Phoenix', 'Baltimore'): 36.25
    }

如何以优雅的方式实现这一点?

使用DataFrame.stack for MultiIndex Series, assign repeated values of Pr Unit Cos and then Series.to_dict:

#remove not necessary columns in final dict
df1 = df.drop(['Capacity','Pr Unit Cos'], axis=1)

s = df1.stack(dropna=False)
d = (pd.Series(np.repeat(df['Pr Unit Cos'].to_numpy(), 
              len(df1.columns)), index=s.index)
       .to_dict())