I want to set the first N rows of a pandas dataframe that satisfy a condition. Eg for:
data = {'id':[1,1,2,2,2,4,2,2], 'val':[9,9,9,9,9,9,9,9]}
df = pd.DataFrame(data)
If I do:
df.loc[df.id == 2] = None
I set all rows with id == 2, but I only want to set N of them.
I've tried
df.loc[df.id == 2][range(N)] = None
but to no avail.
Is there any way to do this, without using loops?