【问题标题】:Get index of a row of a pandas dataframe as an integer以整数形式获取 pandas 数据框行的索引
【发布时间】:2017-05-04 04:38:09
【问题描述】:

假设一个简单的数据框,例如

    A         B
0   1  0.810743
1   2  0.595866
2   3  0.154888
3   4  0.472721
4   5  0.894525
5   6  0.978174
6   7  0.859449
7   8  0.541247
8   9  0.232302
9  10  0.276566

在给定条件的情况下,如何检索行的索引值? 例如: dfb = df[df['A']==5].index.values.astype(int) 返回[4],但我想得到的只是4。这在后面的代码中给我带来了麻烦。

根据某些条件,我希望记录满足该条件的索引,然后选择其中的行。

我试过了

dfb = df[df['A']==5].index.values.astype(int)
dfbb = df[df['A']==8].index.values.astype(int)
df.loc[dfb:dfbb,'B']

想要的输出

    A         B
4   5  0.894525
5   6  0.978174
6   7  0.859449

但我得到TypeError: '[4]' is an invalid key

【问题讨论】:

标签: python pandas numpy


【解决方案1】:

更简单的是添加[0] - 用一个元素选择列表的第一个值:

dfb = df[df['A']==5].index.values.astype(int)[0]
dfbb = df[df['A']==8].index.values.astype(int)[0]

dfb = int(df[df['A']==5].index[0])
dfbb = int(df[df['A']==8].index[0])

但如果可能某些值不匹配,则会引发错误,因为第一个值不存在。

如果值不匹配,解决方案是使用 nextiter 获取默认参数:

dfb = next(iter(df[df['A']==5].index), 'no match')
print (dfb)
4

dfb = next(iter(df[df['A']==50].index), 'no match')
print (dfb)
no match

那么好像需要减法1:

print (df.loc[dfb:dfbb-1,'B'])
4    0.894525
5    0.978174
6    0.859449
Name: B, dtype: float64

boolean indexingquery 的另一种解决方案:

print (df[(df['A'] >= 5) & (df['A'] < 8)])
   A         B
4  5  0.894525
5  6  0.978174
6  7  0.859449

print (df.loc[(df['A'] >= 5) & (df['A'] < 8), 'B'])
4    0.894525
5    0.978174
6    0.859449
Name: B, dtype: float64

print (df.query('A >= 5 and A < 8'))
   A         B
4  5  0.894525
5  6  0.978174
6  7  0.859449

【讨论】:

    【解决方案2】:

    要回答有关如何将索引作为所需选择的整数的原始问题,以下将起作用:

    df[df['A']==5].index.item()
    

    【讨论】:

      【解决方案3】:

      想要包含 A == 5 所在的行和所有行但包括 A == 8 的行意味着我们最终将使用 iloc 的行的性质(loc 包括两者切片的末端)。

      为了获得索引标签,我们使用idxmax。这将返回最大值的第一个位置。我在一个布尔系列上运行它,其中A == 5(然后是A == 8)返回A == 5第一次发生时的索引值(A == 8也是如此)。

      然后我使用searchsorted 找到索引标签(我在上面找到的)出现的顺序位置。这是我在iloc 中使用的。

      i5, i8 = df.index.searchsorted([df.A.eq(5).idxmax(), df.A.eq(8).idxmax()])
      df.iloc[i5:i8]
      


      numpy

      您可以通过使用底层 numpy 对象和类似的 numpy 函数来进一步增强这一点。我把它包装成一个方便的函数。

      def find_between(df, col, v1, v2):
          vals = df[col].values
          mx1, mx2 = (vals == v1).argmax(), (vals == v2).argmax()
          idx = df.index.values
          i1, i2 = idx.searchsorted([mx1, mx2])
          return df.iloc[i1:i2]
      
      find_between(df, 'A', 5, 8)
      


      时机

      【讨论】:

        【解决方案4】:

        按行搜索的小总结:

        如果您不知道列值或列具有非数字值,这可能很有用

        如果你想把索引号作为整数你也可以这样做:

        item = df[4:5].index.item()
        print(item)
        4
        

        它也适用于 numpy / list:

        numpy = df[4:7].index.to_numpy()[0]
        lista = df[4:7].index.to_list()[0]
        

        在 [x] 中,你选择 [4:7] 范围内的数字,例如如果你想要 6:

        numpy = df[4:7].index.to_numpy()[2]
        print(numpy)
        6
        

        对于数据框:

        df[4:7]
        
            A          B
        4   5   0.894525
        5   6   0.978174
        6   7   0.859449
        

        或:

        df[(df.index>=4) & (df.index<7)]
        
            A          B
        4   5   0.894525
        5   6   0.978174
        6   7   0.859449   
        

        【讨论】:

          猜你喜欢
          • 2013-05-09
          • 1970-01-01
          • 1970-01-01
          • 2022-06-29
          • 2021-03-27
          • 1970-01-01
          • 2013-08-14
          • 2021-08-14
          • 2020-10-22
          相关资源
          最近更新 更多