我有一个df(Apple_farm
),需要根据在两个列(Good_apples
和Total_apples
)中找到的值计算一个百分比,然后将得到的值添加到Apple嫒u farm中名为“Perc嫒Good”的新列中。
我试过:
Apple_farm['Perc_Good'] = (Apple_farm['Good_apples'] / Apple_farm['Total_apples']) *100
但是,这会导致此错误:
TypeError: unsupported operand type(s) for /: 'str' and 'str'
做
Print Apple_farm['Good_apples']
和Print Apple_farm['Total_apples']
生成一个包含数值的列表,但是将它们分开似乎会导致它们转换为字符串?
我还试图定义一个新函数:
def percentage(amount, total):
percent = amount/total*100
return percent
但不确定如何使用。
任何帮助都将不胜感激,因为我是相当新的Python和熊猫!
我认为您需要将
string
列转换为float
或int
,因为它们的type
是string
(但看起来像数字):样品:
相关问题 更多 >
编程相关推荐