2

I am very new to pandas and trying to get the row index for the any value higher than the lprice. Can someone give me a quick idea on what I am doing wrong?

Dataframe

      StrikePrice
0      40.00
1      50.00
2      60.00
3      70.00
4      80.00
5      90.00
6     100.00
7     110.00
8     120.00
9     130.00
10    140.00
11    150.00
12    160.00
13    170.00
14    180.00
15    190.00
16    200.00
17    210.00
18    220.00
19    230.00
20    240.00

Now I am trying to figure out how to get the row index for any value which is higher than the lprice

 lprice = 99
 for strike in df['StrikePrice']:
     strike = float(strike)
     # print(strike)
      if strike >= lprice:
          print('The high strike is:' + str(strike))
          ce_1 = strike
          print(df.index['StrikePrice' == ce_1])

The above gives 0 as the index

I am not sure what I am doing wrong here.

Sid
  • 3,749
  • 7
  • 29
  • 62
  • Your two (so far) answers both deal with slicing a dataframe based on a column and then getting at the index. This post may also be useful. https://stackoverflow.com/q/17071871/2336654 – piRSquared Mar 18 '18 at 07:49
  • @piRSquared thanks, that link is going to be very useful. – Sid Mar 18 '18 at 07:51

3 Answers3

4

Using the index attribute after boolean slicing.

lprice = 99
df[df.StrikePrice >= lprice].index

Int64Index([6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20], dtype='int64')

If you insist on iterating and finding when you've found it, you can modify your code:

lprice = 99
for idx, strike in df['StrikePrice'].iteritems():
    strike = float(strike)
    # print(strike)
    if strike >= lprice:
        print('The high strike is:' + str(strike))
        ce_1 = strike
        print(idx)
piRSquared
  • 285,575
  • 57
  • 475
  • 624
  • Thanks. Is it possible to get the index value in the code above i.e. to get it for only the value being iterated over? I need to get corresponding value from a different column using the same. – Sid Mar 18 '18 at 07:49
  • I Updated my post. Hopefully that answers your question. – piRSquared Mar 18 '18 at 07:52
3

I think best is filter index by boolean indexing:

a = df.index[df['StrikePrice'] >= 99]

#alternative
#a = df.index[df['StrikePrice'].ge(99)]

Your code should be changed similar:

lprice = 99
for strike in df['StrikePrice']:
    if strike >= lprice:
        print('The high strike is:' + str(strike))
        print(df.index[df['StrikePrice'] == strike])
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
2

numpy.where(condition[, x, y]) does exactly this if we specify only condition.

np.where() returns the tuple condition.nonzero(), the indices where condition is True, if only condition is given.

In [36]: np.where(df.StrikePrice >= lprice)[0]
Out[36]: array([ 6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20], dtype=int64)

PS thanks @jezrael for the hint -- np.where() returns numerical index positions instead of DF index values:

In [41]: df = pd.DataFrame({'val':np.random.rand(10)}, index=pd.date_range('2018-01-01', freq='9999S', periods=10))

In [42]: df
Out[42]:
                          val
2018-01-01 00:00:00  0.459097
2018-01-01 02:46:39  0.148380
2018-01-01 05:33:18  0.945564
2018-01-01 08:19:57  0.105181
2018-01-01 11:06:36  0.570019
2018-01-01 13:53:15  0.203373
2018-01-01 16:39:54  0.021001
2018-01-01 19:26:33  0.717460
2018-01-01 22:13:12  0.370547
2018-01-02 00:59:51  0.462997

In [43]: np.where(df['val']>0.5)[0]
Out[43]: array([2, 4, 7], dtype=int64)

workaround:

In [44]: df.index[np.where(df['val']>0.5)[0]]
Out[44]: DatetimeIndex(['2018-01-01 05:33:18', '2018-01-01 11:06:36', '2018-01-01 19:26:33'], dtype='datetime64[ns]', freq=None)
MaxU - stand with Ukraine
  • 205,989
  • 36
  • 386
  • 419