Search code examples
pythonpandasindexingis-empty

return zero instead of nothing when no rows in the corresponding index, and slicing from a list of tuples


I have two questions: the first one: I have the following code

count_by_week=df[df['institution_id']==1].groupby(['group_ident','Year','Week'],observed=True, as_index=True)['activity_id','Teacher_ID','school_subject'].nunique()
g=[2332136, 2192823, 2192825]
Y21=[2021]
Y22=[2022]
l21=list(range(30,53,1))
l22=list(range(1,10,1))
for i in g:
    lw=list(itertools.product(i,Y21,l21))+list(itertools.product(i,Y22,l22))
    count_by_week.loc[count_by_week.index.isin(lw),:] 

I want the following line count_by_week.loc[count_by_week.index.isin(lw),:] to return 0 instead of nothing when no item in the corresponding year and week.

and the second one : if I have the following list of tuple with three elements (Id, Year,Week) :

[ (2192825, 2021, 45),
 (2192825, 2021, 46),
 (2192825, 2021, 47),
 (2192825, 2021, 48),
 (2192825, 2021, 49),
 (2192825, 2021, 50),
 (2192825, 2021, 51),
 (2192825, 2021, 52),
 (2192825, 2022, 1),
 (2192825, 2022, 2),
 (2192825, 2022, 3),
]

and I want to get the items between week 50 and week 2. is there a way for this? I tried the following assuming that the above list called lw0 :

lw0[lw0.index(week1):lw0.index(week2)]

this hadn't works! Thanks for any suggestion


Solution

  • I figured out the solution for the first question: the last line should be modified be using reindex() function:

    count_by_week.loc[count_by_week.index.isin(lw),:].reindex((i for i in lw),fill_value=0) 
    

    this will make the code return zero when no instances in the corresponding indexes.