我有一个 pandas 数据框
矩阵["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')
根据条件替换值的最佳方法是什么?
最佳答案
更新:
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
关于python - 在条件语句后设置数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41491536/