如何提取float系列熊猫的str值?

时间:2020-08-20 04:31:04

标签: python pandas

我正在处理由float值和Pandas.Series类型组成的庞大数据系列。

我在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)

但是,我遇到以下错误。

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

此错误提到数据系列可能包含str类型的数据。

我想提取并删除这些数据。

我该怎么做?

1 个答案:

答案 0 :(得分:3)

pd.to_numeric与选项errors='coerce'dropna一起使用

示例:

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