Question
When we store values such as True
or False
into a dataframe, are they stored as strings?
Answer
No, although the values may seem to be string types when printing out a dataframe, the values of True
and False
are stored as the bool
type.
To see the data types that each column of your dataframe stores, you can utilize the .info()
method, as covered in the previous lesson on Pandas.
One thing to keep in mind is that when inspecting the column data types using .info()
, you will see types such as float64
, bool
, as well as object
. The object
data type means that the column can store any Python object. Columns that store more than one type of value, say a column that contains numbers and strings, will have a dtype
of object
.
6 Likes
My understanding was that in pandas dataframes, there can only be once type per column. However, this answer says otherwise. Am I missing something?
You can select a number of different dtypes as the answer above mentions, with the option to use the object dtype which then allows any type of object to be stored (only possible with that specific column type).
However, you often want to perform operations on all the rows in a column, if they all have different types then that may not be possible (even when it is you may lose some of the vectorisation improvements gained from using a numpy array).
So if you can, use the correct data type for the best results and use the object dtype only if you have to.
1 Like
Hi, I tried, but got object not bool. Any ideas why ?
0 Product ID 4 non-null int64
1 Description 4 non-null object
2 Cost to Manufacture 4 non-null float64
3 Price 4 non-null float64
4 Sold in Bulk? 4 non-null object