1

I have the following Dataframe:

data = {'level1':[20,19,20,21,25,29,30,31,30,29,31],
        'level2': [10,10,20,20,20,10,10,20,20,10,10]}
index = pd.date_range('12/1/2014', periods=11)
frame = pd.DataFrame(data, index=index)

In [75]: frame
Out[75]:
level1  level2
2014-12-01  20  10
2014-12-02  19  10
2014-12-03  20  20
2014-12-04  21  20
2014-12-05  25  20
2014-12-06  29  10
2014-12-07  30  10
2014-12-08  31  20
2014-12-09  30  20
2014-12-10  29  10
2014-12-11  31  10

I would like to take a "cell" of this frame, and find out which row it is in.

In other words, frame.level1[4]."rownumber"()?

What is the integer row number? The desired answer would be 4

Note: I am aware this is circular, the ultimate goal is to put this into a formula to pick up another "cell" using relative navigation, e.g.

#pseudocode
def rowgetter(x)
    priorvalue =frame.at[index[rownumber(x)-1],'level1']#pseudocode
    priorvalue += 10 #or something

and then
rownmums=frame.level1.map(rowgetter)

Result would look like

nan(?)
30
29
30
31
35
39
40
41
40
39

The reason I'm asking this is that I can't get shift to work in a formula, but even if the question in the link is answered, I find this question interesting enough by itself.

Community
  • 1
  • 1
DISC-O
  • 300
  • 1
  • 3
  • 13

1 Answers1

0

You could use argwhere:

In [11]: np.argwhere((frame.level1 == 25).values)[0][0]
Out[11]: 4

or argmax:

In [12]: np.argmax((frame.level1 == 25).values)
Out[12]: 4

Note: this wouldn't raise if no match was found, but you could wrap it in something to check (that the value at this position was 25).

I don't think there's a way to lookup a specific value without == (I guess it's similar to this question.

Community
  • 1
  • 1
Andy Hayden
  • 359,921
  • 101
  • 625
  • 535
  • Thanks Andy, but my need is slightly different, I am not going to pass in the actual value (e.g. 25) but the data frame element, and all inside a function. I added that to the question, maybe it wasn't clear. the other issue is that my values are not unique, so I couldn't look up the unique row for the value 30 in my example (as it is twice in frame.level1) – DISC-O Feb 07 '15 at 18:21