【发布时间】:2017-02-03 20:18:20
【问题描述】:
我在数据框中有几列包含数值和字符串
我想删除所有字符,只留下数字
Admit_DX_Description Primary_DX_Description
510.9 - EMPYEMA W/O FISTULA 510.9 - EMPYEMA W/O FISTULA
681.10 - CELLULITIS, TOE NOS 681.10 - CELLULITIS, TOE NOS
780.2 - SYNCOPE AND COLLAPSE 427.89 - CARDIAC DYSRHYTHMIAS NEC
729.5 - PAIN IN LIMB 998.30 - DISRUPTION OF WOUND, UNSPEC
到
Admit_DX_Description Primary_DX_Description
510.9 510.9
681.10 681.10
780.2 427.89
729.5 998.30
代码:
for col in strip_col:
# # Encoding only categorical variables
if df[col].dtypes =='object':
df[col] = df[col].map(lambda x: x.rstrip(r'[a-zA-Z]'))
print df.head()
错误:
Traceback(最近一次调用最后一次):
df[col] = df[col].map(lambda x: x.rstrip(r'[a-zA-Z]'))
文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/core/series.py”,第 2175 行,在地图中 new_values = map_f(值,arg) 文件“pandas/src/inference.pyx”,第 1217 行,在 pandas.lib.map_infer (pandas/lib.c:63307)
df[col] = df[col].map(lambda x: x.rstrip(r'[a-zA-Z]'))
AttributeError: 'int' 对象没有属性 'rstrip'
【问题讨论】: