【发布时间】:2016-08-18 10:59:00
【问题描述】:
我在 Pandas 中有一个数据框,其中列出了如下信息:
Player Year Height
1 Stephen Curry 2015-16 6-3
2 Mirza Teletovic 2015-16 6-10
3 C.J. Miles 2015-16 6-7
4 Robert Covington 2015-16 6-9
现在 data['Height'] 将其值存储为字符串,我想将这些值转换为英寸存储为整数以供进一步计算。
我尝试了一些方法,包括 Pandas 文档中列出的方法,但都无济于事。
第一次尝试
def true_height(string):
new_str = string.split('-')
inches1 = new_str[0]
inches2 = new_str[1]
inches1 = int(inches1)*12
inches2 = int(inches2)
return inches1 + inches2
如果你运行
true_height(data.iloc[0, 2])
它返回 75,即正确答案。
为了在整个系列中运行它,我更改了这行代码:
new_str = string.**str**.split('-')
然后跑:
data['Height'].apply(true_height(data['Height']))
并收到以下错误消息:
int() argument must be a string or a number, not 'list'
然后我尝试使用 for 循环,认为这可能会解决问题,因此我将原始公式修改为:
def true_height(strings):
for string in strings:
new_str = string.split('-')
inches1 = new_str[0]
inches2 = new_str[1]
inches1 = int(inches1)*12
inches2 = int(inches2)
return inches1 + inches2
现在我收到以下错误:
'int' object is not callable
当我跑步时:
data['Height'].apply(true_height(data['Height']))
我有点难过。任何帮助,将不胜感激。谢谢。
【问题讨论】:
-
我希望你在
def true_height(strings):行之后缩进。