AttributeError: type object 'str' has no attribute

huangapple go评论78阅读模式
英文:

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)

huangapple
  • 本文由 发表于 2023年4月17日 21:07:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76035491-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定