从数据框索引值中删除字符串

时间:2018-11-20 18:45:34

标签: numpy

我想从索引值中删除字符串:

df.index.get_values().str.replace("and over", "").astype(int)

这样做将返回以下错误:

AttributeError: 'numpy.ndarray' object has no attribute 'str'

我已经尝试找到numpy使用的类似函数来实现此目的,但我似乎找不到任何函数。这是我的索引值:

['0' '1' '2' '3' '4' '5' '6' '7' '8' '9' '10' '11' '12' '13' '14' '15'
 '16' '17' '18' '19' '20' '21' '22' '23' '24' '25' '26' '27' '28' '29'
 '30' '31' '32' '33' '34' '35' '36' '37' '38' '39' '40' '41' '42' '43'
 '44' '45' '46' '47' '48' '49' '50' '51' '52' '53' '54' '55' '56' '57'
 '58' '59' '60' '61' '62' '63' '64' '65' '66' '67' '68' '69' '70' '71'
 '72' '73' '74' '75' '76' '77' '78' '79' '80' '81' '82' '83' '84' '85'
 '86' '87' '88' '89' '90 and over' 'All ages']

1 个答案:

答案 0 :(得分:0)

类似以下代码的东西应该可以工作。但是首先,您必须删除“所有年龄段”条目。

arr = np.array([x.replace(' and over', '') for x in arr]).astype(int)