使用if-else创建新列时出现Pandas错误:系列的真值是模糊的[重复]

lnlaulya  于 2023-09-29  发布在  其他
关注(0)|答案(5)|浏览(121)

此问题已在此处有答案

Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()(14个回答)
Create new column based on values from other columns / apply a function of multiple columns, row-wise in Pandas(8个回答)
4天前关闭。
我正在使用Pandas,并试图使用Python if-else语句(又名三元条件运算符)创建一个新列,以避免被零除。
例如下面的例子,我想通过除以A/B来创建一个新的列C。我想使用if-else语句来避免除以0。

  1. import pandas as pd
  2. import numpy as np
  3. df = pd.DataFrame(np.random.randint(0, 5, size=(100, 2)), columns=list('AB'))
  4. df.head()
  5. # A B
  6. # 0 1 3
  7. # 1 1 2
  8. # 2 0 0
  9. # 3 2 1
  10. # 4 4 2
  11. df['C'] = (df.A / df.B) if df.B > 0.0 else 0.0

但是,我从最后一行得到一个错误:

  1. ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

我在StackOverflow上搜索了一下,发现了其他关于这个错误的帖子,但是没有一个涉及这种类型的if-else语句。一些职位包括:
Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()
The truth value of a Series is ambiguous in dataframe
Error: The truth value of a Series is ambiguous - Python pandas
如果你能帮忙的话,我将不胜感激。

idv4meu8

idv4meu81#

那做什么呢

  1. >>> df['C'] = np.where(df.B>0., df.A/df.B, 0.)

其内容如下:
如果df.B为正,则返回df.A/df.B,否则返回0.

0qx6xfy6

0qx6xfy62#

df.B > 0产生一个系列,例如:

  1. 0 True # 4 > 0 => True
  2. 1 True # 2 > 0 => True
  3. 2 True # ...
  4. 3 True
  5. 4 True
  6. 5 True
  7. 6 True
  8. 7 True
  9. 8 False # 0 is not > 0 => False
  10. 9 False # 0 is not > 0 => False
  11. ...

返回多个值,这会导致歧义(一些值为True,而另一些值为False)。
一个解决方案是使用np.where

  1. sentinel = np.nan # Or 0 if you must...
  2. df = df.assign(C=np.where(df['B'] != 0, df['A'] / df['B'], sentinel))
  3. >>> df
  4. A B C
  5. 0 2 4 0.5
  6. 1 0 2 0.0
  7. 2 1 2 0.5
  8. 3 4 4 1.0
  9. 4 1 1 1.0
  10. 5 4 4 1.0
  11. 6 2 4 0.5
  12. 7 1 2 0.5
  13. 8 4 0 NaN # NaN is assigned in cases where the value in Column `B` is zero.
  14. 9 1 0 NaN
  15. ...
展开查看全部
w41d8nur

w41d8nur3#

基于上面关于迭代 Dataframe 的@vaishnav提案,这里是一个工作提案:

  1. for index, row in df.iterrows():
  2. if row.B > 0:
  3. df.loc[index, 'C'] = row.A / row.B
  4. else:
  5. df.loc[index, 'C'] = 0

输出量:

  1. A B C
  2. 0 3 4 0.750000
  3. 1 0 4 0.000000
  4. 2 4 3 1.333333
  5. 3 2 1 2.000000
  6. 4 1 0 0.000000
  7. 5 0 2 0.000000
oo7oh9g9

oo7oh9g94#

  1. df['C']=df.A.div(df.B.mask(df.B.lt(0),0)).fillna(0)
  2. df
  3. Out[89]:
  4. A B C
  5. 0 1 3 0.333333
  6. 1 1 2 0.500000
  7. 2 0 0 0.000000
  8. 3 2 1 2.000000
  9. 4 4 2 2.000000

使用apply lambda

  1. df['C']=df.apply(lambda x : x['A']/x['B'] if x['B']>0 else 0,1)
  2. df
  3. Out[93]:
  4. A B C
  5. 0 1 3 0.333333
  6. 1 1 2 0.500000
  7. 2 0 0 0.000000
  8. 3 2 1 2.000000
  9. 4 4 2 2.000000
展开查看全部
sxpgvts3

sxpgvts35#

或者你可以打开一个for循环。

  1. for i,j in df['a'],df['b']:
  2. if j>0:
  3. df['c']=i/j
  4. else:
  5. df['c']=0.0

相关问题