python - 在条件语句后设置数据框

标签 python pandas

我有一个 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/

相关文章:

python - 实现模式匹配 - 何时使用缓存?

python - 将列表作为行和列索引添加到 pandas 数据框

python - 尝试访问 pandas 数据框中新分配的列时出现 KeyError

python - 从受密码保护的 Excel 文件到 pandas DataFrame

python - Pandas eval - 在列上调用用户定义的函数

python-3.x - 在 Python 中比较和排序列之间的值(使用 "Not Matched"列)

python - 在 valgrind 下运行 python 显示很多内存错误是否正常?

python - 如何使用 django allauth 实现邀请流程以进行注册/登录?

python - 如何从运行在 Google App Engine SDK 上的 Python 应用程序访问本地 MySQL 实例?

python - 如何展平数据框中的列