python - If, And Statement 没有在 Python 中输出准确的结果

标签 python if-statement jupyter-notebook

df.loc[(df['Platform'] == 'A321') and (df['Days'] <= 14), 'Mods Applicable'] = 'CFM56 9th Stage Duct'

我试图让 A321 的平台和天数小于或等于 14 的输出打印出以下“适用的模组”,但它没有输出平台和天数。

输出结果:

Output results

IF 和输出:

IF & Output

最佳答案

尝试使用“&”而不是“and”:

df = pd.DataFrame({'Platform': ['A123', 'A321', 'A321', 'B123'],
                   'Days': [10, 13, 20, 5]})

df.loc[(df['Platform'] == 'A321') & (df['Days'] <= 14), 'Mods Applicable'] = 'CFM56 9th Stage Duct'

print(df)

      Platform  Days       Mods Applicable
    0     A123    10                   NaN
    1     A321    13  CFM56 9th Stage Duct
    2     A321    20                   NaN
    3     B123     5                   NaN

我可以通过使用“and”而不是“&”来重现相同的错误:

Traceback (most recent call last):

  File "<ipython-input-128-7957a219684b>", line 1, in <module>
    df.loc[(df['Platform'] == 'A321') and (df['Days'] <= 14), 'Mods Applicable'] = 'CFM56 9th Stage Duct'

  File "/Users/nathanielgates/anaconda3/lib/python3.6/site-packages/pandas/core/generic.py", line 1479, in __nonzero__
    .format(self.__class__.__name__))

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

关于python - If, And Statement 没有在 Python 中输出准确的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55189069/

相关文章:

c - 在 C 中是否分配了指针?

javascript - 我的 JavaScript 函数/if 语句仅在按特定顺序放置时才有效

arrays - Swift 3.0 Xcode 中是否有类似于 "If Any"的语句

python - Jupyter笔记本: let a user inputs a drawing

python - python 内置方法是否在任何地方的替代 namespace 中可用?

python - (相机校准)类型错误:参数 '%s'的预期Ptr <cv::UMat>

python - int() 参数必须是字符串、类似字节的对象或数字,而不是 'QueryDict'

python - 使用 Django View 将每 3 个对象分组到单独的上下文变量中?

docker - 无法从 docker 访问 jupyter notebook

python2.7 : matplotlib in jupyter notebook can't use qt