I'm looping thru a dataset trying to find values in an index. If the value isn't in the (multi-level) index, then I take a default value:
for i,row in df.iterrows():
if i in avg_days.index:
df.at[i,'avg_days_to_n'] = round(avg_days.xs(i,axis=0)[1],0)
else:
df.at[i,'avg_days_to_n'] = round(avg_days.xs((i[0],i[1]),level (0,1),drop_level=False).avg_days_between_n.mean(),0)
is there a way to vectorize this?
Here is what the avg_days dataframe looks like:
avg_n
state project_type cat
FL R Hur 33
CA C EE 43
Here is what df looks like:
id
state project_type cat
FL R Hur abc-1
CA C EE def-2
So, for each row in df, if the index state-project_type-cat has an index match in avg_days, I return the avg_n value.
If not, i "move up" the index and take the state-project type (and NOT cat) value as i know that will return something