ValueError: invalid literal for int() with base 10: '<NA>'。

问题描述 投票:0回答:1

有一个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") 但它不工作。

python pandas
1个回答
0
投票

让我们试试 pd.to_numeric

df["counts"] = pd.to_numeric(df["counts"],errors='coerce')

df["counts"]=df["counts"].astype(object)
© www.soinside.com 2019 - 2024. All rights reserved.