Python: How to iterate over the indexes of a variable in a 'for loop'

Somehow iterating over a multi-indexed dataframe can be tricky.
I wanted to access a value related to an employee in the current and in the three subsequent periods of time. I needed to test the value of each period in an 'if statement' that would decide the next employee's task (scheduling problem). However, the if condition returned a Series (containing the index levels) instead of a single value and then the program was not able to give a True/False response. And the program crashed.

So so weird, but it turns out, it was just a syntax issue.
#The current and subsequent periods
p_prox = [16 17 18 19]

#b_atual is a magnitude to compare the "value" associated to each employee in each period of time. Let's suppose b_atual = 5.

#iterator
p_count = 0

#dataframe 'rank'
rank = 
 employee  Period  value
 Juli       16      9
 Juli       17      9
 Juli       18      5
 Juli       19      9

#This is the loop:
for p in p_prox:    
    while p_count < 6: 
        if rank.loc[(employee,p)].iloc[0] == b_atual:
            task[employee][p] =  1
        p_count = p_count + 1

Long story short, this is how we access a single value of a multi-indexed dataframe:
df.loc[(index1,index2)].iloc[0]
Note that we are in fact accessing the value in the first row of the returned Series. But as long as we know there will be only one value for each employee in each period of time, it works well.

GAMS: how to limit elements of a set when defining constraints

I had this multiperiod problem in which one of the constraints should be build over all periods of time, except for the last one.
As I had P = {1,2,3,4,5} periods of time, it meant that this particular constraint existed for the subgroup P' = {1,2,3,4}, while other constraints considered the whole time horizon.

Simple thing, hah?

Then I'd found myself wasting a lot of time to figure out how to code it on GAMS, despite of the wide availability of GAMS material on-line.
In the end, it was actually simple:

You have to define a subset before stating the constraints, as follow:

//Define the set and its respective subset 
SET T PERIODS /T1,T2,T3,T4,T5/;
SET g(T);

//Limit the subset accordingly. In this case, ORD(T) refers to the relative position of an element in the set.
//Thus, ORD(T)<5 means that the new subset g(T) will contain the first 4 elements of set T, aka.(T1, T2, T3, T4).
g(T) = ORD(T)<5   ; 

//Declare the variables and parameters (ommited here)
//Declare the objective function (ommited here)

//Declare the constraints as usual. Only substitute the set by the appropriated subset, when needed.
  FLOW(J,K,g(T))..                SUM((I), X(I,J,K,T)) =E= SUM((I), X(J,I,K,T+1)) ;  

I tried other ways to sum it up, but none of them worked.

If there is another manner to do it, please let me know.

Python: how to obtain nlargest in a multiindex DataFrame/Series

A brief tutorial on how to identify the top n elements of each group in a dataset. 

First, I grouped my data as follow:
df_group = df_train.groupby(['SaleCondition', 'Neighborhood'])['SalePrice'].sum()
Result:
# SaleCondition  Neighborhood
# Abnorml        BrDale            288900
#                BrkSide           309600
#                ClearCr           505000
#                CollgCr           562900
#                Crawfor           587000
#                Edwards           831900
#                Gilbert           181000
#                IDOTRR            499887
#                MeadowV            92000
#                Mitchel           417686
#                NAmes            3070950
#                NPkVill           140000
#                NWAmes            993000
#                NoRidge          1603000
#                OldTown          1180680
#                SWISU             489434
#                Sawyer            728300
#                SawyerW           739400
#                Somerst           791552
#                StoneBr           187500
#                Timber            599500
# AdjLand        Edwards           416500
# Alloca         Crawfor           559724
#                Edwards           453970
#                IDOTRR             55993
#                Mitchel           206300
#                OldTown            89471
#                Sawyer            108959
#                SawyerW           534112
# Family         BrDale             88000
#                                   ...   
# Normal         Gilbert         12121140
#                IDOTRR           3148700
#                MeadowV          1583800
#                Mitchel          6527250
#                NAmes           29211643
Then, we filter only the largest values for each SaleCondition:
df_group.groupby(level=0, group_keys=False).nlargest(5)
Result:
# SaleCondition  Neighborhood
# Abnorml        NAmes            3070950
#                NoRidge          1603000
#                OldTown          1180680
#                NWAmes            993000
#                Edwards           831900
# AdjLand        Edwards           416500
# Alloca         Crawfor           559724
#                SawyerW           534112
#                Edwards           453970
#                Mitchel           206300
#                Sawyer            108959
# Family         NAmes             533000
#                Gilbert           484000
#                OldTown           473000
#                NWAmes            404500
#                Crawfor           393500
# Normal         NAmes           29211643
#                CollgCr         25010162
#                NridgHt         12827100
#                OldTown         12518308
#                NWAmes          12403155
# Partial        NridgHt         11525738
#                Somerst          7920842
#                CollgCr          4121804
#                StoneBr          3337049
#                Gilbert          2449366
This must be useful to build Pareto charts.

Python: How to iterate over the indexes of a variable in a 'for loop'

Somehow iterating over a multi-indexed dataframe can be tricky. I wanted to access a value related to an employee in the current and in the...