[英]Remove non-numeric in df column with different datatypes
我有一个这样的 dataframe:
Id Volume
1 350 L
2 250.0
3 150//
4 250 L
我想删除 Volume 列中的非数字。 愿望 output 是:
Id Volume
1 350
2 250
3 150
4 250
我尝试使用df['Volume'] = df['Volume'].str.extract('(\d+)', expand=False)
但它会变成 '250.0' 和 '150//' 值南。
我也尝试过使用df['Volume'] = df['Volume'].str[:3]
但它也会将 '250.0' 和 '150//' 值变为 nan。
我还尝试将列 dtypes 更改为字符串,但没有用。 它仍然是 object 数据类型。
这应该有效: df['Volumne'] = df['Volume'].str.replace(r'[^0-9.]', '')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.