在 df 上应用嵌套的 if 语句

Apply nested if statement on df

以下代码必须执行几个步骤:

  1. if column 'wholesale data' usage >= column 'mbs' than
  2. 减去列 'wholesale data' - 'mbs'
  3. 根据下面乘以差值table

    • 32 - 0.56
    • 64 - 0.5
    • 128 - 0.4
    • 256 - 0.4
    • 512 - 0.3
    • 1024 - 0.3
  4. 对于所有其他可能的选项 - 0

我得到了以下代码

if df['Wholesale Data Usage'] >= df['mbs']:
    if df['mbs'] == "32":
        df['Additional Charge'] = (df['Wholesale Data Usage'] - df[
        'mbs']) * 0.5
    elif df['mbs'] == "64":
        df['Additional Charge'] = (df['Wholesale Data Usage'] - df[
        'mbs']) * 0.5
    elif df['mbs'] == "128":
        df['Additional Charge'] = (df['Wholesale Data Usage'] - df[
        'mbs']) * 0.4
    elif df['mbs'] == "256":
        df['Additional Charge'] = (df['Wholesale Data Usage'] - df[
        'mbs']) * 0.4
    elif df['mbs'] == "512":
        df['Additional Charge'] = (df['Wholesale Data Usage'] - df[
        'mbs']) * 0.3
    elif df['mbs'] == "1024":
        df['Additional Charge'] = (df['Wholesale Data Usage'] - df[
        'mbs']) * 0.3
    else:
        df['Additional Charge'] = 0
else:
    df['Additional Charge'] = 0

不确定我做错了什么。

首先,对于 32,您乘以 0.5 而不是 0.56。

那么,你好像把StringsIntegers搞混了。如果存储在 df['Wholesale Data Usage']df['mbs'] 中的数据是 String 类型,那么您不能检查是否大于 (>=),也不能从另一个中减去一个。

我怀疑字典中存储的数据类型是IntegerFloatDouble.在这种情况下,您正在尝试将数据与字符串进行比较,并且应该更改以下内容:

if df['mbs'] == "32":

至:

if df['mbs'] == 32:

每次比较都做同样的事情

您的代码很难阅读,也很难调试。我认为编码风格将帮助您看到自己的错误。如果这样写会怎样?

mbs = df['mbs']
wdu = df['Wholesale Data Usage']

ac = 0
if wdu >= mbs and mbs == "32"   : ac = (wdu - mbs) * 0.56
if wdu >= mbs and mbs == "64"   : ac = (wdu - mbs) * 0.5
if wdu >= mbs and mbs == "128"  : ac = (wdu - mbs) * 0.4
if wdu >= mbs and mbs == "256"  : ac = (wdu - mbs) * 0.4
if wdu >= mbs and mbs == "512"  : ac = (wdu - mbs) * 0.3
if wdu >= mbs and mbs == "1024" : ac = (wdu - mbs) * 0.3

df['Additional Charge'] = ac

也就是说你遇到了类型问题。我第一个相等 mbs=="32" 你正在比较,就好像你有一个字符串。然后在等式中,您尝试将这些值相加,就好像它们包含一个 int 一样——这是行不通的。让类型保持一致。

mbs = float(df['mbs'])
wdu = float(df['Wholesale Data Usage'])

ac = 0
if wdu >= mbs and mbs == 32   : ac = (wdu - mbs) * 0.56
if wdu >= mbs and mbs == 64   : ac = (wdu - mbs) * 0.5
if wdu >= mbs and mbs == 128  : ac = (wdu - mbs) * 0.4
if wdu >= mbs and mbs == 256  : ac = (wdu - mbs) * 0.4
if wdu >= mbs and mbs == 512  : ac = (wdu - mbs) * 0.3
if wdu >= mbs and mbs == 1024 : ac = (wdu - mbs) * 0.3

df['Additional Charge'] = str(ac) #<- you need to know what the expected type is here. 

我认为您的逻辑和代码单步执行逻辑的方式存在一些问题,例如您检查 mbs 列是否等于 32,但如果 Wholesale Data Usage 列和 mbs 列之间的差异等于 32,那么它实际上应该等于 32。

这应该可以在不编辑太多代码的情况下修复您的逻辑:

df['mbs'] = np.where(df['Wholesale Data Usage'] >= df['mbs'], df['Wholesale Data Usage']-df['mbs'], 0)

if df['mbs'] == "32":
    df['Additional Charge'] = (df['mbs']) * 0.5
elif df['mbs'] == "64":
    df['Additional Charge'] = (df['mbs']) * 0.5
elif df['mbs'] == "128":
    df['Additional Charge'] = (df['mbs']) * 0.4
elif df['mbs'] == "256":
    df['Additional Charge'] = (df['mbs']) * 0.4
elif df['mbs'] == "512":
    df['Additional Charge'] = (df['mbs']) * 0.3
elif df['mbs'] == "1024":
    df['Additional Charge'] = (df['mbs']) * 0.3
else:
    df['Additional Charge'] = 0