Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

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

I executed the 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 am getting the following error.

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

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

I would like to extract and remove this data.

How can I do that?

1 Answer

0 votes
by (36.8k points)

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

 Do check out Data Science with Python course which helps you understand from scratch 

Browse Categories

...