I have a multi indexed pandas table as below.
I want to update Crop and Avl column, say with 'Tomato', and '0', but only for finite no of times (say, I need only 10 rows for Tomato, satisfying a condition). Currently via pandas I end up updating all rows that satisfy that condition.
col1 = ildf1.index.get_level_values(1) # https://stackoverflow.com/a/50608928/9148067
cond = col1.str.contains('DN_Mega') & (ildf1['Avl'] == 1)
ildf1.iloc[ cond , [0,2]] = ['Tomato', 0]
How do I restrict it to only say 10 rows of all rows that satisfy the condition?
PS: I used get_level_values
as I have 4 columns (GR, PP+MT, Bay, Row) multi indexed in my df.