I have a Pandas dataframe with a column whose dtype is object
, and when I print the dataframe content, the column show the value True
, but I can't tell if such value is a boolean or a string (both are possible).
How can I find it out?
I have a Pandas dataframe with a column whose dtype is object
, and when I print the dataframe content, the column show the value True
, but I can't tell if such value is a boolean or a string (both are possible).
How can I find it out?
If you want it all printed out you could just check the exact type of teach element by applying type
function with map
or apply
x = pd.DataFrame([True, 'True', 0, False])
x[0].map(type)
All you need to know: How can I know the type of a pandas dataframe cell
(Not a duplicate because that question does not mention booleans in particular.)
For example:
df = pd.DataFrame({'numeric' : [1, 1., 2, 3],
'objects' : [1, True, 'True', False]})
df
numeric objects
0 1.0 1
1 1.0 True
2 2.0 True
3 3.0 False
df.applymap(type)
numeric objects
0 <class 'float'> <class 'int'>
1 <class 'float'> <class 'bool'>
2 <class 'float'> <class 'str'>
3 <class 'float'> <class 'bool'>
<class 'bool'> vs <class 'str'>:
... will allow you to cast the type function by cells instead of getting the column dtype.