簡體   English   中英

Pandas 查詢時返回 nan

[英]Pandas returns nan when queried

這是我在使用時遇到此問題的代碼:

def getBill(basket):
    if len(basket) != 0:
        for index in range(len(df)):
            if df.iloc[index]["amount"] != df.iloc[index]["amountPayable"]:
                name = "     Promotion " + df.iloc[index]["promotion"] #The spacing is done for formatting   purposes 
                row = pd.DataFrame({"name": name, "price": price, "unit": unit, "amount": amount}, index=[index + 0.5]) #I have declared all the variables correctly
                df = df.append(row, ignore_index=False)
                df = df.sort_index().reset_index(drop=True) #This is what breaks the code

回報:

name = "     Promotion " + df.iloc[index]["promotion"]
TypeError: can only concatenate str (not "float") to str

這是因為df.iloc[index]["promotion"]index = 2處為nan ,但是在打印時 dataframe 籃子返回這些值:

  promotion  price  amount  amountPayable
0      None   2.00    0.55           0.55
1  get4pay3   0.50    5.00           4.00
2  get4pay3   1.25    2.00           0.00
3  get4pay3  10.00   10.00           9.00
4  get2pay1   1.75    9.00           5.00

問題應該是由於重置索引而發生的(我正在嘗試使用此方法插入中間: 是否可以使用 Z3A43B4F88325D94022C0EFA9C2FA2F 在 dataframe 中的任意 position 處插入一行? )。 什么可能導致這個問題?

在連接之前轉換為字符串:

name = "     Promotion " + str(df.iloc[index]["promotion"])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM