繁体   English   中英

如何在pandas列中找到行索引?

[英]How to find the row index in pandas column?

我对pandas很新,并试图获得任何高于lprice value的行index 有人可以让我快速了解我做错了什么吗?

数据帧

      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

现在我想弄清楚如何获得任何higher lprice valuerow index

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

以上给出0作为index

我不确定我在这里做错了什么。

在布尔切片后使用index属性。

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

如果你坚持迭代并找到你找到它的时候,你可以修改你的代码:

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)

我认为最好是通过boolean indexing过滤boolean indexing

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

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

您的代码应该更改类似:

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

如果我们只指定condition ,numpy.where(condition [,x,y])就是这样做的。

np.where()返回元组condition.nonzero()condition为true的索引,如果只给出condition

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感谢@jezrael提示 - np.where()返回数字索引位置而不是DF索引值:

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)

解决方法:

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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