简体   繁体   中英

How to find the row index in pandas column?

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.

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)

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])

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM