0

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?

HuLu ViCa
  • 5,077
  • 10
  • 43
  • 93
  • Does this answer your question? [How can I know the type of a pandas dataframe cell](https://stackoverflow.com/questions/49926897/how-can-i-know-the-type-of-a-pandas-dataframe-cell) – wjandrea Aug 01 '23 at 23:37

2 Answers2

0

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)
Mikael Öhman
  • 2,294
  • 15
  • 21
0

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'>:

  • map (for a series),
  • applymap (for a dataframe)

... will allow you to cast the type function by cells instead of getting the column dtype.

OCa
  • 298
  • 2
  • 13
  • 1
    This is a duplicate as far as I'm concerned. Particular types don't make a difference in that respect, otherwise we could have questions asking, "How do I know if this is a list?", "How do I know if this is a datetime?", etc. – wjandrea Aug 01 '23 at 23:40