问题描述
是否有更好的方法可以将对象数据类型转换为fillna值:
df =df.fillna({"first_col":0.0,"second_col":0.0})
解决方法
我首先将其转换为int类型,然后转换为对象类型。
# float (-> fillna) -> int -> object
# converting to int type
df["first_col"] = df["first_col"].astype("int")
df["second_col"] = df["second_col"].astype("int")
# converting to obj. type
df["first_col"] = df["first_col"].astype("object")
df["second_col"] = df["second_col"].astype("object")
,
我认为您需要检查downcast
参数Dataframe.fillna