找到 pandas.Series 中的所有非数字元素
Locate all non-number elements in a pandas.Series
对于混合字符串和数字(整数和浮点数)的 pd.Series,我需要识别所有非数字元素。例如
data = pd.Series(['1','wrong value','2.5','-3000','>=50','not applicable', '<40.5'])
我想要return以下元素:
wrong value
>=50
not applicable
<40.5
我目前正在做的是:
data[~data.str.replace(r'[\.\-]','').str.isnumeric()]
也就是说,因为.str.isnumeric()
会给小数点和负号False
,所以我不得不屏蔽“。”和“-”,然后找出非数字字段。
有更好的方法吗?或者我目前的方法有什么潜力 problem/warning 吗?谢谢!!
使用pd.to_numeric
标记他们
data[pd.to_numeric(data, errors='coerce').isna()]
Out[1159]:
1 wrong value
4 >=50
5 not applicable
6 <40.5
dtype: object
对于混合字符串和数字(整数和浮点数)的 pd.Series,我需要识别所有非数字元素。例如
data = pd.Series(['1','wrong value','2.5','-3000','>=50','not applicable', '<40.5'])
我想要return以下元素:
wrong value
>=50
not applicable
<40.5
我目前正在做的是:
data[~data.str.replace(r'[\.\-]','').str.isnumeric()]
也就是说,因为.str.isnumeric()
会给小数点和负号False
,所以我不得不屏蔽“。”和“-”,然后找出非数字字段。
有更好的方法吗?或者我目前的方法有什么潜力 problem/warning 吗?谢谢!!
使用pd.to_numeric
标记他们
data[pd.to_numeric(data, errors='coerce').isna()]
Out[1159]:
1 wrong value
4 >=50
5 not applicable
6 <40.5
dtype: object