3

I have data similar to this post: pandas: Filling missing values within a group

That is, I have data in a number of observation sessions, and there is a focal individual for each session. That focal individual is only noted once, but I want to fill in the focal ID data for each line during that session. So, the data look something like this:

     Focal    Session
0    NaN      1
1    50101    1
2    NaN      1
3    NaN      2
4    50408    2
5    NaN      2

Based on the post linked above, I was using this code:

g = data.groupby('Session')

g['Focal'].transform(lambda s: s.loc[s.first_valid_index()])

But this returns a KeyError (specifically, KeyError:None). According to the .loc documentation, KeyErrors can result when the data isn't found. So, I've checked and while I have 152 sessions, I only have 150 non-null data points in the Focal column. Before I decide to manually search my data for which of the sessions is missing a Focal ID, I have two questions:

  1. I am very much a beginner. So is this a reasonable explanation for why I am getting a KeyError?

  2. If it is reasonable, is there a way to figure out which Session is missing Focal ID data, that will save me from manually looking through the data?

Output here:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-330-0e4f27aa7e14> in <module>()
----> 1 data['Focal'] = g['Focal'].transform(lambda s: s.loc[s.first_valid_index()])
      2 g['Focal'].transform(lambda s: s.loc[s.first_valid_index()])

//anaconda/lib/python2.7/site-packages/pandas/core/groupby.pyc in transform(self, func,     *args, **kwargs)
   1540         for name, group in self:
   1541             object.__setattr__(group, 'name', name)
-> 1542             res = wrapper(group)
   1543             # result[group.index] = res
   1544             indexer = self.obj.index.get_indexer(group.index)

//anaconda/lib/python2.7/site-packages/pandas/core/groupby.pyc in <lambda>(x)
   1536             wrapper = lambda x: getattr(x, func)(*args, **kwargs)
   1537         else:
-> 1538             wrapper = lambda x: func(x, *args, **kwargs)
   1539 
   1540         for name, group in self:

<ipython-input-330-0e4f27aa7e14> in <lambda>(s)
----> 1 data['Focal'] = g['Focal'].transform(lambda s: s.loc[s.first_valid_index()])
      2 g['Focal'].transform(lambda s: s.loc[s.first_valid_index()])

//anaconda/lib/python2.7/site-packages/pandas/core/indexing.pyc in __getitem__(self, key)
    669             return self._getitem_tuple(key)
    670         else:
--> 671             return self._getitem_axis(key, axis=0)
    672 
    673     def _getitem_axis(self, key, axis=0):

//anaconda/lib/python2.7/site-packages/pandas/core/indexing.pyc in _getitem_axis(self, key, axis)
    756             return self._getitem_iterable(key, axis=axis)
    757         else:
--> 758             return self._get_label(key, axis=axis)
    759 
    760 class _iLocIndexer(_LocationIndexer):

//anaconda/lib/python2.7/site-packages/pandas/core/indexing.pyc in _get_label(self, label, axis)
     58             return self.obj._xs(label, axis=axis, copy=False)
     59         except Exception:
---> 60             return self.obj._xs(label, axis=axis, copy=True)
     61 
     62     def _get_loc(self, key, axis=0):

//anaconda/lib/python2.7/site-packages/pandas/core/series.pyc in _xs(self, key, axis, level, copy)
    570 
    571     def _xs(self, key, axis=0, level=None, copy=True):
--> 572         return self.__getitem__(key)
    573 
    574     def _ixs(self, i, axis=0):

//anaconda/lib/python2.7/site-packages/pandas/core/series.pyc in __getitem__(self, key)
    611     def __getitem__(self, key):
    612         try:
--> 613             return self.index.get_value(self, key)
    614         except InvalidIndexError:
    615             pass

//anaconda/lib/python2.7/site-packages/pandas/core/index.pyc in get_value(self, series, key)
    761         """
    762         try:
--> 763             return self._engine.get_value(series, key)
    764         except KeyError, e1:
    765             if len(self) > 0 and self.inferred_type == 'integer':

//anaconda/lib/python2.7/site-packages/pandas/index.so in pandas.index.IndexEngine.get_value (pandas/index.c:2565)()

//anaconda/lib/python2.7/site-packages/pandas/index.so in pandas.index.IndexEngine.get_value (pandas/index.c:2380)()

//anaconda/lib/python2.7/site-packages/pandas/index.so in pandas.index.IndexEngine.get_loc (pandas/index.c:3166)()

KeyError: None
4

2 回答 2

1

问题是,first_valid_index如果没有有效值(DataFrame 中的某些组都是 NaN),则返回 None:

In [1]: s = pd.Series([np.nan])

In [2]: s.first_valid_index() # None

现在,loc由于没有索引而引发错误None

In [3]: s.loc[s.first_valid_index()]
KeyError: None

在这种特殊情况下,您希望您的代码做什么?...
如果你希望它是 NaN,你可以回填然后取第一个元素:

g['Focal'].transform(lambda s: s.bfill().iloc[0])
于 2013-09-24T06:01:29.047 回答
0

如果您想解决某些组仅包含 Nan 的问题,您可以执行以下操作:

  1. g = data.groupby('会话')
  2. g['Focal'].transform(lambda s: '没有要聚合的值' if pd.isnull(s).all() == True else s.loc[s.first_valid_index()])
  3. df['Focal'] = g['Focal'].transform(lambda s: '没有要聚合的值' if pd.isnull(s).all() == True else s.loc[s.first_valid_index()] )

这样,当程序找到特定组的所有 Nan 时,您输入“没有要聚合的值”(或任何您想要的),而不是阻止执行以返回错误。

希望这可以帮助 :)

费德里科

于 2017-11-21T16:06:04.137 回答