0

I have dataframe as bellow , I need to get the index of first changing value 0 in column y

x1  x2  y
12  7   1
0   3   0
5   11  1
7   2   1
4   8   1
1   0   0
5   0   0
0   9   0
2   2   1

So it would be some thing like this

x1  x2  y
0   3   0
1   0   0
   

I have tried this but it just print out the index where elements change

print(df.iloc[:,2].diff()[df.iloc[:,2].diff() != 0].index.values)

Thanks

sweet1908
  • 1
  • 1
  • What have you tried until now? – JenilDave Aug 06 '20 at 10:19
  • 1
    The logic is not entirely clear to me. Why `0 3 0` and `2 2 1` are not in your expected result? Why `12 7 0` and not `12 7 1`? – alec_djinn Aug 06 '20 at 10:23
  • up to now I'm still looking for the answer but I tried to get the index only by doing as bellow `print(df[2].diff()[df[2].diff() != 0].index.values)` but it gives all the index where elements change value but not specific '0' value @JeniDave – sweet1908 Aug 06 '20 at 10:57
  • @alec_djinn I have reference to this question [link](https://stackoverflow.com/questions/19125661/find-index-where-elements-change-value-numpy) – sweet1908 Aug 06 '20 at 11:20
  • @sweet1908 Then you have your question answered already there. pandas.DataFrame columns can be cast to numpy array, in your case `df.y.values`. – alec_djinn Aug 06 '20 at 11:27

0 Answers0