Prasad Prasad - 2 months ago 5
Python Question

Index values of specific rows in python

I am trying to find out Index of such rows before "None" occurs.

pId=["a","b","c","None","d","e","None"]
df = pd.DataFrame(pId,columns=['pId'])

pId
0 a
1 b
2 c
3 None
4 d
5 e
6 None

df.index[df.pId.eq('None') & df.pId.ne(df.pId.shift(-1))]


I am expecting the output of the above code should be

Index([2,5])


It gives me

Index([3,6])


Please correct me

GpG GpG
Answer

I am not sure for the specific example you showed. Anyway, you could do it in a more simple way:

indexes = [i-1 for i,x in enumerate(pId) if x == 'None']
Comments