Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting float value to a bool pandas column without conversion to float

Tags:

python

pandas

df2 = pd.DataFrame({'test':[False,True,True]})
df2.loc[0,'Test']=np.nan

The above result df converting the 'test' column to floats, where the True's get converting into 1.0. Is it possible to set this value, keeping the dtype of the column to obj so I can mix float and bool's, i.e.

    test
0   NaN
1   True
2   True
like image 208
Cr1064 Avatar asked Oct 18 '25 11:10

Cr1064


1 Answers

dtype=object

Yes. You need to tell Pandas that the column is of dtype object

pd.DataFrame({'test':[False,True,True]}, dtype=object)

If altering an existing dataframe

df2['test'] = df2['test'].astype(object)
like image 68
piRSquared Avatar answered Oct 20 '25 00:10

piRSquared



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!