I have a pandas Series containing multiple different datatypes. I want to filter out all elements which are not numeric. The resulting Series should only contain floats or ints.
Is there a simple way to filter the Series? Most solutions I found only worked with DataFrames.
UPDATE:
In [43]: s
Out[43]:
0 0
1 1
2 str1
3 NaN
4 3
5 5
6 str2
7 4
8 NaN
dtype: object
convert to numeric:
In [44]: pd.to_numeric(s, errors='coerce')
Out[44]:
0 0.0
1 1.0
2 NaN
3 NaN
4 3.0
5 5.0
6 NaN
7 4.0
8 NaN
dtype: float64
drop NaNs:
In [45]: pd.to_numeric(s, errors='coerce').dropna()
Out[45]:
0 0.0
1 1.0
4 3.0
5 5.0
7 4.0
dtype: float64
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.