Pandas 将混合数据类型列转换为浮点数

Pandas converting mixed datatype column to float

我有一个包含一列的数据框,如下所示:

df['mixed'].values
array([' GC', '345', '69', '28-'], dtype=object)

我尝试了以下方法:

df['mixed'].astype(float, errors='ignore')

但它什么也没做。我期待以下内容:

df['mixed'].values
array([' GC', 345, 69, '28-'], dtype=object)

你可以做到pd.to_numeric

pd.to_numeric(df['mixed'],errors='coerce').fillna(df['mixed']).tolist()
[' GC', 345.0, 69.0, '28-']

如果转换过程中出现错误,将返回原始对象。 documentation for astype [强调我的]:

errors{‘raise’, ‘ignore’}, default ‘raise’ Control raising of exceptions on invalid data for provided dtype.

  • raise : allow exceptions to be raised
  • ignore : suppress exceptions. On error return original object.

要获得预期的输出,您可以改用 to_numeric and setting errors='coerce' which will set any invalid rows to NaN. These can then be set to the original value using fillna ().