[英]Pandas returns nan when queried
This is my code while working with which I encountered this problem:这是我在使用时遇到此问题的代码:
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
Returns:回报:
name = " Promotion " + df.iloc[index]["promotion"]
TypeError: can only concatenate str (not "float") to str
This occurs due to df.iloc[index]["promotion"]
being nan
at index = 2
, however when printed the dataframe basket returns these values:这是因为
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
The problem should occur due to resetting indexes (I am trying to insert into the middle using this method: Is it possible to insert a row at an arbitrary position in a dataframe using pandas? ).问题应该是由于重置索引而发生的(我正在尝试使用此方法插入中间: 是否可以使用 Z3A43B4F88325D94022C0EFA9C2FA2F 在 dataframe 中的任意 position 处插入一行? )。 What could be causing this problem?
什么可能导致这个问题?
Convert to string before concatenating:在连接之前转换为字符串:
name = " Promotion " + str(df.iloc[index]["promotion"])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.