applymap - 使用指数分布的值填充数据框

时间:2016-04-05 22:34:05

标签: python pandas

我想用指数分布的值填充我的数据框。据我所知,可以这样做:

cumLosses1 = pd.DataFrame(np.zeros((size,size)))
cumLosses1.applymap(np.random.exponential(scale = 1.0))

如文档中所述,applymap将函数应用于旨在按元素运行的DataFrame - 这正是我想要的 但是,我明白了:

TypeError                                 Traceback (most recent call last)
<ipython-input-50-fa00d9fe4e79> in <module>()
     13 cumLosses1 = pd.DataFrame(np.zeros((size,size)))
     14 
---> 15 cumLosses1.applymap(np.random.exponential(scale = 1.0))
     16 #print cumLosses1

/home/alexdenis/anaconda2/lib/python2.7/site-packages/pandas/core/frame.pyc in applymap(self, func)
   4141                 x = lib.map_infer(_values_from_object(x), f)
   4142             return lib.map_infer(_values_from_object(x), func)
-> 4143         return self.apply(infer)
   4144 
   4145     #----------------------------------------------------------------------

/home/alexdenis/anaconda2/lib/python2.7/site-packages/pandas/core/frame.pyc in apply(self, func, axis, broadcast, raw, reduce, args, **kwds)
   3970                     if reduce is None:
   3971                         reduce = True
-> 3972                     return self._apply_standard(f, axis, reduce=reduce)
   3973             else:
   3974                 return self._apply_broadcast(f, axis)

/home/alexdenis/anaconda2/lib/python2.7/site-packages/pandas/core/frame.pyc in _apply_standard(self, func, axis, ignore_failures, reduce)
   4062             try:
   4063                 for i, v in enumerate(series_gen):
-> 4064                     results[i] = func(v)
   4065                     keys.append(v.name)
   4066             except Exception as e:

/home/alexdenis/anaconda2/lib/python2.7/site-packages/pandas/core/frame.pyc in infer(x)
   4140                 f = com.i8_boxer(x)
   4141                 x = lib.map_infer(_values_from_object(x), f)
-> 4142             return lib.map_infer(_values_from_object(x), func)
   4143         return self.apply(infer)
   4144 

pandas/src/inference.pyx in pandas.lib.map_infer (pandas/lib.c:62578)()

TypeError: ("'float' object is not callable", u'occurred at index 0')

我的错误可能是什么?

1 个答案:

答案 0 :(得分:0)

np.random.exponential(scale=1.0)无法调用。你想要:

lambda x: np.random.exponential(scale=1.0),可以被称为&#34;&#34;对于系列中的每个元素,即使不使用这些元素。

但是,要做到这一点,它会更加复杂:

cumLosses1 = pd.DataFrame(np.random.exponential(scale=1.0, size=(N, N)))

N是你的身材。