我有一个pandas数据帧
matrices["dist"]
1 2
1 2.92 70.75
2 71.34 5.23
我尝试使用apply
matrices["dist"].apply(lambda x: 1 if x >= 50 else 0)
但是我收到了错误
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-272-bb796b629822> in <module>()
----> 1 matrices["dist"].apply(lambda x: 1 if x >= 1 else 0)
C:\Anaconda\lib\site-packages\pandas\core\frame.pyc in apply(self, func, axis, broadcast, raw, reduce, args, **kwds)
4150 if reduce is None:
4151 reduce = True
-> 4152 return self._apply_standard(f, axis, reduce=reduce)
4153 else:
4154 return self._apply_broadcast(f, axis)
C:\Anaconda\lib\site-packages\pandas\core\frame.pyc in _apply_standard(self, func, axis, ignore_failures, reduce)
4246 try:
4247 for i, v in enumerate(series_gen):
-> 4248 results[i] = func(v)
4249 keys.append(v.name)
4250 except Exception as e:
<ipython-input-272-bb796b629822> in <lambda>(x)
----> 1 matrices["dist"].apply(lambda x: 1 if x >= 1 else 0)
C:\Anaconda\lib\site-packages\pandas\core\generic.pyc in __nonzero__(self)
915 raise ValueError("The truth value of a {0} is ambiguous. "
916 "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
--> 917 .format(self.__class__.__name__))
918
919 __bool__ = __nonzero__
ValueError: ('The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().', u'occurred at index 1')
最好的方法是替换条件下的值吗?
答案 0 :(得分:2)
<强>更新强>
In [121]: ((df >= 50) & (df <= 71)) * 1
Out[121]:
1 2
1 0 1
2 0 0
试试这个:
In [106]: (df>=50).astype(int)
Out[106]:
1 2
1 0 1
2 1 0
或
In [107]: (df>=50) * 1
Out[107]:
1 2
1 0 1
2 1 0