我在df中有一列叫做size
df['Size']
0 19M
1 14
2 8.7
3 25
4 2.8M
5 5.6
我想删除此列中的所有M,所以我做了
df.Size.str.replace('M','')
它起作用了,但是我也想将该列中的字符串转换为float。
我尝试了df.Size.float.replace('M','')
但是我收到此错误:
AttributeError:“系列”对象没有属性“浮动”
我该怎么办?
答案 0 :(得分:1)
我正在使用to_numeric
更新
pd.to_numeric(df.Size.replace('M','',regex=True),errors='coerce').fillna(df.Size)
Out[497]:
0 19
1 14k
2 8.7
3 25
4 2.8
5 5.6
Name: Size, dtype: object
在此处检查转换,仅该单元格仍包含k个str
类型,所有其他单元格变为float
pd.to_numeric(df.Size.replace('M','',regex=True),errors='coerce').fillna(df.Size).apply(type)
Out[501]:
0 <class 'float'>
1 <class 'str'>
2 <class 'float'>
3 <class 'float'>
4 <class 'float'>
5 <class 'float'>
Name: Size, dtype: object
数据输入
df
Out[500]:
Size
0 19M
1 14k
2 8.7
3 25
4 2.8M
5 5.6
答案 1 :(得分:0)
为了安全起见,我们可以使用jmp *%rax
删除所有字母:
regex