2016-03-31 178 views
2

我有一个df(Apple_farm),需要计算两列(Good_applesTotal_apples)中找到的基于关闭值的百分比,然后将结果值添加到Apple_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']

产生具有但是数值除以他们的列表似乎导致他们成为c倒转到字符串?

我也试图定义一个新的功能:

def percentage(amount, total): 
    percent = amount/total*100 
    return percent 

,但如何使用这个不确定。

任何帮助将不胜感激,因为我是相当新的Python和熊猫!

回答

2

我想你需要转换stringfloatint,因为他们的typestring(不过貌似号):

Apple_farm['Good_apples'] = Apple_farm['Good_apples'].astype(float) 
Apple_farm['Total_apples'] = Apple_farm['Total_apples'].astype(float) 

Apple_farm['Good_apples'] = Apple_farm['Good_apples'].astype(int) 
Apple_farm['Total_apples'] = Apple_farm['Total_apples'].astype(int) 

样品:

import pandas as pd 

Good_apples = ["10", "20", "3", "7", "9"] 
Total_apples = ["20", "80", "30", "70", "90"] 
d = {"Good_apples": Good_apples, "Total_apples": Total_apples} 
Apple_farm = pd.DataFrame(d) 
print Apple_farm 
    Good_apples Total_apples 
0   10   20 
1   20   80 
2   3   30 
3   7   70 
4   9   90 

print Apple_farm.dtypes 
Good_apples  object 
Total_apples object 
dtype: object 

print Apple_farm.at[0,'Good_apples'] 
10 

print type(Apple_farm.at[0,'Good_apples']) 
<type 'str'> 
Apple_farm['Good_apples'] = Apple_farm['Good_apples'].astype(int) 
Apple_farm['Total_apples'] = Apple_farm['Total_apples'].astype(int) 

print Apple_farm.dtypes 
Good_apples  int32 
Total_apples int32 
dtype: object 

print Apple_farm.at[0,'Good_apples'] 
10 

print type(Apple_farm.at[0,'Good_apples']) 
<type 'numpy.int32'> 
Apple_farm['Perc_Good'] = (Apple_farm['Good_apples']/Apple_farm['Total_apples']) *100 

print Apple_farm 
    Good_apples Total_apples Perc_Good 
0   10   20  50.0 
1   20   80  25.0 
2   3   30  10.0 
3   7   70  10.0 
4   9   90  10.0