我怎么做df [“col1”]。astype(float),默认为不可转换的值?

时间:2017-12-06 18:04:27

标签: python pandas

这是我的代码

$number = preg_replace('/\D/', '', $query); // replaces every non number for empty string if (is_numeric($number)) { $output = ''; // return variations like you want foreach (array ('weeks', 'years') as $period) { $output .= $number . $period; } return $output; }

col1具有无法转换的值。

我将如何做这样的事情:

f["col1"] = df["col1"].astype(float)

1 个答案:

答案 0 :(得分:1)

您可以使用pd.to_numeric + fillna

df["col1"]=pd.to_numeric(df["col1"], errors='coerce', downcast='float').fillna(0)