Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从数据帧生成传输模型的字典_Python_Pandas_Dataframe_Numpy_Dictionary - Fatal编程技术网

Python 从数据帧生成传输模型的字典

Python 从数据帧生成传输模型的字典,python,pandas,dataframe,numpy,dictionary,Python,Pandas,Dataframe,Numpy,Dictionary,我有一个运输问题的数据框架 Unnamed: 0 Unnamed: 1 c1 c2 c3 c4 c5 capacity 0 NaN p1 4 5 6 8 10 500.0 1 NaN p2 6 4 3 5 8 500.0 2 NaN p3 9 7 4 2 4 500.0 3

我有一个运输问题的数据框架

Unnamed: 0 Unnamed: 1    c1   c2   c3   c4   c5  capacity
0        NaN         p1   4    5    6    8   10     500.0
1        NaN         p2   6    4    3    5    8     500.0
2        NaN         p3   9    7    4    2    4     500.0
3     demand        NaN  80  270  250  160  180       NaN
我像这样更改了列名

df.columns = ['Demand', 'Plant', 'c1', 'c2', 'c3', 'c4', 'c5', 'capacity']
 d = {c1:80, c2:270, c3:250, c4:160, c5:180}  # customer demand
 M = {p1:500, p2:500, p3:500}               # factory capacity
 I = [c1,c2,c3,c4,c5]                         # Customers
 J = [p1,p2,p3]                             # Factories
 cost = {(p1,c1):4,    (p1,c2):5,    (p1,c3):6,
 (p1,c4):8,    (p1,c5):10, ......
  } 
我想做一本这样的字典

df.columns = ['Demand', 'Plant', 'c1', 'c2', 'c3', 'c4', 'c5', 'capacity']
 d = {c1:80, c2:270, c3:250, c4:160, c5:180}  # customer demand
 M = {p1:500, p2:500, p3:500}               # factory capacity
 I = [c1,c2,c3,c4,c5]                         # Customers
 J = [p1,p2,p3]                             # Factories
 cost = {(p1,c1):4,    (p1,c2):5,    (p1,c3):6,
 (p1,c4):8,    (p1,c5):10, ......
  } 
对于第一种情况,我使用了以下代码

  M = df.set_index('Plant')['capacity'].to_dict()
它给了我

  {'p1': 500.0, 'p2': 500.0, 'p3': 500.0, nan: nan}  
我不想要任何价值观。请帮助以通用方式查找此总词典(d、M和cost),无需NaN

df1 = df.set_index(["Unnamed: 0", "Unnamed: 1"])
plants = df1.loc[np.NaN]  # remove demand from dataframe

d = dict(df1.loc["demand"].T.squeeze().dropna().iteritems())
M = dict(plants["capacity"].iteritems())
I = list(plants.drop(columns="capacity").columns)
J = list(plants.index)
cost = dict(plants.drop(columns="capacity").stack().iteritems())