有一个pandas数据框,包含一列Int64类型的 "counts",是可空的整数类型。
country date counts
US 2020-04-05 <NA>
US 2020-04-06 <NA>
US 2020-04-07 <NA>
US 2020-04-05 0
有一部分下游代码要处理这个df,把它胁迫成int。int(counts)
导致这种麻烦的。<NA>
真的是缺少数据,我无法填写0。
有什么办法可以避免这种情况吗?df["counts"] = df["counts"].astype("Int64")
但它不工作。
让我们试试 pd.to_numeric
df["counts"] = pd.to_numeric(df["counts"],errors='coerce')
或
df["counts"]=df["counts"].astype(object)