ken
ken

Reputation: 668

How to extract str value in float Series of Pandas?

I'm handling huge data series which are consist of float values and Pandas.Series type.

I executed following code in Python.

import pandas as pd

# Read the specific column from CSV file.
float_log_series = pd.read_csv('./data.csv', usecols=['float_log']).float_log
data_cut = pd.cut(float_log_series, 20)

However, I got following error.

TypeError: '<=' not supported between instances of 'float' and 'str'

This error mentions that the data series could include str type data.

I would like to extract and remove this data.

How can I do that ?

Upvotes: 1

Views: 60

Answers (1)

Andy L.
Andy L.

Reputation: 25239

Use pd.to_numeric with option errors='coerce' and dropna

Sample:

s = pd.Series(['a', 1, 3.4, 'c', 0, 2.0])

Out[24]:
0      a
1      1
2    3.4
3      c
4      0
5      2
dtype: object

s_out = pd.to_numeric(s, errors='coerce').dropna()

Out[29]:
1    1.0
2    3.4
4    0.0
5    2.0
dtype: float64

Upvotes: 3

Related Questions