英文:
AttributeError: type object 'str' has no attribute
问题
First I created a pandas Dataframe called 'inventory' from a csv file
instock = lambda x: True if inventory.quantity > 0 else False
inventory['in_stock'] = inventory.quantity.apply(str.instock)
I tried to create a new column with boolean values, True for quantity > 0
else False
When I try to print the updated dataframe I get the AttributeError
I can't figure out what I'm doing wrong, can someone help me to understand how the library is working in this case?
Shouldn't the variable instock be a boolean? It looks like it's treated like a generic object
英文:
First I created a pandas Dataframe called 'inventory' from a csv file
instock = lambda x: True if inventory.quantity>0 else False
inventory['in_stock'] = inventory.quantity.apply(str.instock)
I tried to create a new column with boolean values, True for quantity > 0
else False
When I try to print the updated dataframe I get the AttributeError
I can't figure out what I'm doing wrong, can someone help me to understand how the library is working in this case?
Shouldn't the variable instock be a boolean? It looks like it's treated like a generic object
答案1
得分: 2
以下是翻译好的部分:
Lambda function here is not necessary, comapre column for greater like 0
for vectorized solution:
这里不需要使用 Lambda 函数,可以比较列中的值是否大于 0
以实现向量化解决方案:
inventory['in_stock'] = inventory.quantity > 0
For learning purpose:
用于学习目的:
instock = lambda x: x > 0
inventory['in_stock'] = inventory.quantity.apply(instock)
英文:
Lambda function here is not necessary, comapre column for greater like 0
for vectorized solution:
inventory['in_stock'] = inventory.quantity > 0
For learning purpose:
instock = lambda x: x>0
inventory['in_stock'] = inventory.quantity.apply(instock)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论