Skip to main content
 首页 » 编程设计

python之是否有一个 Python 函数可以根据条件过滤并将二进制值分配给数据集中的列

2025年05月04日13artech

<分区>

过滤我的结果数据框时出现问题。我的数据集中有一列名为 PaymentAmount其中有数字数据,想执行一个操作来赋值

  • 1 如果data['PaymentAmount'] > 25000和
  • 0 如果data['PaymentAmount'] <= 25000

我尝试使用下面的

1 if data['PaymentAmount'] >= 25000 else 0 

但出现以下错误:

--------------------------------------------------------------------------- 
ValueError                                Traceback (most recent call last) 
<ipython-input-176-e368653724d0> in <module> 
----> 1 1 if data['PaymentAmount'] >= 25000 else 0 
 
~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\generic.py in __nonzero__(self) 
   1574         raise ValueError("The truth value of a {0} is ambiguous. " 
   1575                          "Use a.empty, a.bool(), a.item(), a.any() or a.all()." 
-> 1576                          .format(self.__class__.__name__)) 
   1577  
   1578     __bool__ = __nonzero__ 
 
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().