df2 = pd.DataFrame({'test':[False,True,True]})
df2.loc[0,'Test']=np.nan
上面的结果 df 将“test”列转换为 float ,其中 True 被转换为 1.0。是否可以设置此值,将列的 dtype 保留为 obj,以便我可以混合 float 和 bool,即
test
0 NaN
1 True
2 True
最佳答案
dtype=object
是的。您需要告诉 Pandas 该列是 dtype
对象
pd.DataFrame({'test':[False,True,True]}, dtype=object)
如果更改现有数据框
df2['test'] = df2['test'].astype(object)
关于python - 将 float 值设置为 bool pandas 列而不转换为 float,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58527925/