繁体   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