2017-02-15 49 views
0

語句下面的代碼必須執行幾個步驟:應用嵌套如果上DF

  1. 如果列「批發數據」用法> =列「MBS」比
  2. 減去列「批發數據」 - 「MBS 「
  3. 乘法基於下表

    • 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 

不知道我在做什麼wr翁。

+0

縮進是錯誤的開始和結束。您似乎有兩個'else'語句,但使用不正確的縮進很難知道。你可能會考慮爲表格使用字典,它會更簡單。 – cdarke

回答

0

您一定代碼這麼難閱讀其難以調試。編碼風格將幫助您查看我認爲自己的錯誤。如果你是這樣寫的呢?

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. 
0

首先,對於32,你乘以0.5而不是0.56。

然後,看起來你已經混淆了字符串整數。如果存儲在df['Wholesale Data Usage']df['mbs']中的數據是String類型,則不能檢查大於(>=),也不能從另一箇中減去一個。

我懷疑存儲在字典中的數據的類型是整數浮法的。在這種情況下,您要的數據進行比較,以串並應改變如下:

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

到:

if df['mbs'] == 32: 

,做同樣的,每比較

0

我認爲你剛剛得到了一些關於你的邏輯和代碼遍歷邏輯的方式的問題,例如,您檢查mbs列是否等於32,但確實應該是Wholesale Data Usage列和mbs列之間的差值等於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 
+0

我不這麼認爲......總使用量超過了限制,他們收取的費用加上每個單位超出限制的附加費用。所以他的邏輯起作用。 我假設'df ['Total Cost']'是df ['Regualr Charges'] + df ['額外費用'],因此他正在計算上述超額費用。你的邏輯似乎給過去的折扣? – gbtimmon