代码之家  ›  专栏  ›  技术社区  ›  zeeMonkeez

使用非整数索引和groupby应用的数据帧

  •  1
  • zeeMonkeez  · 技术社区  · 4 年前

    我有以下几点 DataFrame 对于非整数索引:

    df = pd.DataFrame(index=[1.0,1.4,2,2.5], data={'a': ['a','b','a','b']})
    
         a
    1.0  a
    1.4  b
    2.0  a
    2.5  b
    

    当我这么做的时候

    df.groupby('a').apply(lambda x: 42)
    

    我得到以下例外:

    Traceback (most recent call last):
      File "/opt/lib/python3.8/site-packages/pandas/core/indexes/base.py", line 2895, in get_loc
        return self._engine.get_loc(casted_key)
      File "pandas/_libs/index.pyx", line 70, in pandas._libs.index.IndexEngine.get_loc
      File "pandas/_libs/index.pyx", line 101, in pandas._libs.index.IndexEngine.get_loc
      File "pandas/_libs/hashtable_class_helper.pxi", line 386, in pandas._libs.hashtable.Float64HashTable.get_item
      File "pandas/_libs/hashtable_class_helper.pxi", line 393, in pandas._libs.hashtable.Float64HashTable.get_item
    KeyError: 0.0
    

    如果我 reset_index() 在分组之前,我得到了预期的结果:

    df.reset_index().groupby('a').apply(lambda x: 42)
    
    Out[130]: 
    a
    a    42
    b    42
    dtype: int64
    

    如何在不重置索引的情况下对组进行分组并应用函数?

    不管它值多少钱,我使用熊猫1.1.3。

    0 回复  |  直到 4 年前