1

I have a problem that drive me crazy: Assume that we have a Dataframe like this

flowdemands = pd.read_excel('allinputs.xlsx', sheet_name='flowdemands')

Flow  Vnf   Demand
3      1         5
4      2        10

I wanna use read this DataFrame in this way..

for f in FlowSet
  for v in VnfSet 
    print('Flow Demand[{},{}]={}'.Format(f,v,FlowDemand(f,v))

I need somtinge Like 2 Dimension Array FlowDemad:

FlowDemand[3,1]=5
FlowDemand[4,2]=10

output should be like this

 FlowDemand[3,1]=5
 FlowDemand[4,2]=10

First dimension should be Flow and second dimension should be Vnf and Value of Array will be Demand(see above table) Assume that we have

FlowSet=[3,4] and VnfSet=[1,2]

Please help me to find appropriate data structure for solving this... Note: These are not my answer: Pandas read in table without headers and this How to iterate over rows in a DataFrame in Pandas because I need to convert or store this dataframe to 2 dimension Array


By using Matrix this problem is solved: FlowDemand=numpy.zeros((n_flowdemands+1,n_flowdemands+1),dtype=int) FlowDemand[flowdemands.Flow,flowdemands.Vnf]=flowdemands.Demand

NimaGhasri
  • 41
  • 5

1 Answers1

1

As @mozway said, you can use iterrows or itertuples to iterate over a pandas DataFrame

flowdemands = pd.read_excel('allinputs.xlsx', sheet_name='flowdemands')

for flow, vnf, demand in flowdemands.itertuples(index=False):
    print('Flow Demand[{},{}]={}'.format(flow, vnf, demand))

Though there are very few cases when you can rely on iterating a DataFrame. Please read this answer for more details.